Home » Bit Hacks(bits,nibbles&bytes) » Removes Duplicates

Removes Duplicates

// how to rempves duplicates from array list and pass these as hashset object


import java.util.HashSet;
import java.util.ArrayList;
public class RemoveDuplicates {
 public static void removeDuplicates(ArrayList list) {
 HashSet set = new HashSet(list);
 list.clear();
 list.addAll(set);
 }

public static void main(String args[]) {
 ArrayList list = new ArrayList();
 list.add("a");
 list.add("b");
 list.add("a");
 list.add("c");
 list.add("b");
 removeDuplicates(list);
 System.out.println("ArrayList : ");
 for (Object data : list) {
 System.out.println(data);
 }
 }
}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: