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 List
s, 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
Multiple Input in Joptionpane.Showinputdialog
How to Use Google Translate API in My Java Application
Why Doesn't a Missing Annotation Cause a Classnotfoundexception at Runtime
How != and == Operators Work on Integers in Java
Are Non-Capturing Groups Redundant
How to Pause and Resume a Thread in Java from Another Thread
Jtable Won't Show Column Headers
Parallel Streams, Collectors and Thread Safety
Java: Starting a New Thread in a Constructor
How to Resize Jlabel Imageicon
Obtaining the Array Class of a Component Type
Byte Array and Int Conversion in Java
How to Convert List to JSON in Java
How to Round a Double to Two Decimal Places in Java
How to Compile a .Java with Support for Older Versions of Java