Java - Removing Duplicates in an 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.

Removing duplicates from an ArrayList?

A solution could be this one. I startet at the end of the list that I don't delete indexes the loop has to visit in the future.

public static void removeDuplicate(ArrayList<Integer> list) {
int i = list.size() - 1;

while (i > -1) {
// check for duplicate
for (int j = 0; j < i; j++) {
if (list.get(i) == list.get(j)) {
// is duplicate: remove
list.remove(i);
break;
}
}
i--;
}
}

Removing duplicates from arraylist

I think this works. You had 2 small errors.

for (int i = 0; i < name.size(); i++)
{
for (int j = i + 1; j < name.size(); j++) // j needs to start at i + 1 not 1.
if (name.get(i).equals(name.get(j)))
{
name.remove(j); // You need to remove at the higher index
name.remove(i); // first, because items are shifted left.
j = j - 1;
}
}

Remove duplicates from ArrayListString[] - java

As @Eran notes, you can't work with arrays directly, since they don't override Object.equals(). Hence, arrays a and b are only equal if they are the same instance (a == b).

It's straightforward to convert the arrays to Lists, which do override Object.equals:

List<String[]> distinct = test.stream()
.map(Arrays::asList) // Convert them to lists
.distinct()
.map((e) -> e.toArray(new String[0])) // Convert them back to arrays.
.collect(Collectors.toList());

Ideone demo

How to remove Duplicate values in ArrayList?

Use HashSet to remove duplicate values:

HashSet hs = new HashSet();
hs.addAll(contact_phoneList); // name of contact_phoneList from which you want to remove duplicates
contact_phoneList.clear();
contact_phoneList.addAll(hs);

No need for a for loop

does ArrayList.removeAll(Collection) cares about duplicates?

As stated in the Java docs:

removeAll(Collection c)

Removes all of this collection's elements that are also contained in
the specified collection (optional operation). After this call
returns, this collection will contain no elements in common with the
specified collection.

Removing Duplicate Integers in an ArrayList

The specific reason why your removeAllDuplicates function doesn't work is that you are still iterating after a successful comparison. If you iterate only when list.get(i) != list.get(i + 1), you will get rid of all the duplicates.

public static ArrayList<Integer> removeAllDuplicates(ArrayList<Integer> list) {
Collections.sort(list);
int i = 0;
while(i < list.size() - 1) {
if (list.get(i) == list.get(i + 1)) {
list.remove(i);
} else {
i++;
}
}
return list;
}

It's worth noting that the above function is not as fast as it could be. Though the iteration runs quickly enough, the most significant step will be the sort operation (O(n log n)).

To avoid this extra time complexity, consider using a HashSet instead of an ArrayList (if it still fits within the constraints of your problem).



Related Topics



Leave a reply



Submit