How to Remove Repeated Elements from Arraylist

How do I remove repeated elements from ArrayList?

If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:

Set<String> set = new HashSet<>(yourList);
yourList.clear();
yourList.addAll(set);

Of course, this destroys the ordering of the elements in the ArrayList.

How to remove duplicate values in ArrayList with original element

Using a hashmap here might make sense. The keys could be the file paths, and the value, if the key be present, would be the number of times the file occurs. Something like this:

Map<String, Integer> map = new HashMap<>();
map.put("C:\123\456\NameOfUniqueFolder0", 1);
map.put("C:\123\456\NameOfUniqueFolder1", 1);
map.put("C:\123\456\NameOfUniqueFolder2", 1);
map.put("C:\123\456\NameOfUniqueFolder3", 1);
map.put("C:\123\456\NameOfUniqueFolder4", 1);

Now when a new path comes along, increment its counter:

String newPath = "C:\123\456\NameOfUniqueFolder0";
Integer val = map.get(newPath);
map.put(newPath, val == null ? 1 : val.intValue() + 1);
}

At the end, you can iterate this map, and check the counter values for each key. You would then only process the files having occurred only once:

for (Map.Entry<String, Integer> entry : map.entrySet()) {
int count = entry.getValue();
if (count == 1) {
// process this file
}
// otherwise skip this path
}

Remove duplicate items in ArrayList using for loop only

You have many issues:

  1. You will remove non-duplicate records when i == j. To avoid that the inner loop should start with int j = i + 1.
  2. When you remove records, you should decrement j, since all the elements following the removed element are shifted one index down.
  3. You should compare Integers with equals, not with ==

Removing Duplicate Values from ArrayList

You can create a LinkedHashSet from the list. The LinkedHashSet will contain each element only once, and in the same order as the List. Then create a new List from this LinkedHashSet. So effectively, it's a one-liner:

list = new ArrayList<String>(new LinkedHashSet<String>(list))

Any approach that involves List#contains or List#remove will probably decrease the asymptotic running time from O(n) (as in the above example) to O(n^2).


EDIT For the requirement mentioned in the comment: If you want to remove duplicate elements, but consider the Strings as equal ignoring the case, then you could do something like this:

Set<String> toRetain = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
toRetain.addAll(list);
Set<String> set = new LinkedHashSet<String>(list);
set.retainAll(new LinkedHashSet<String>(toRetain));
list = new ArrayList<String>(set);

It will have a running time of O(n*logn), which is still better than many other options. Note that this looks a little bit more complicated than it might have to be: I assumed that the order of the elements in the list may not be changed. If the order of the elements in the list does not matter, you can simply do

Set<String> set = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
set.addAll(list);
list = new ArrayList<String>(set);

Remove duplicates from an ArrayList?

Better use a HashSet. If not possible then you can use a temporary HashSet for this.

ArrayList a= new ArrayList();
HashSet hs = new HashSet();
hs.addAll(a); // willl not add the duplicate values
a.clear();
a.addAll(hs); // copy the unique values again to arraylist


Related Topics



Leave a reply



Submit