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
.
Remove duplicate items in ArrayList using for loop only
You have many issues:
- You will remove non-duplicate records when
i == j
. To avoid that the inner loop should start withint j = i + 1
. - When you remove records, you should decrement
j
, since all the elements following the removed element are shifted one index down. - You should compare
Integer
s withequals
, not with==
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
Time complexity of removing duplicates in an ArrayList
The time-complexity of your algorithm is O(n^2)
, since for each element in the array you have to compare it with every single one added before. You can improve that to actually O(n*log(n))
O(n)
using a Set
.
You mentioned that you care about the order and as far as you know, the HashSet
does not maintain it. It's true, but there is a way to make it work. Use LinkedHashSet
like so:
ArrayList<Integer> array =
new ArrayList<>(Arrays.asList(1, 5, 4, 2, 2, 0, 1, 4, 2));
LinkedHashSet<Integer> set = new LinkedHashSet<>(array);
This will construct a LinkedHashSet
, which time-complexity of insertion, deletion and finding is O(1)
. The important part is the difference between this data structure and your typical HashSet
. LinkedHashSet
additionally creates a linked list that indicates the order of the elements in terms of insertion.
If you run the above code with this loop:
for(Integer i : set) {
System.out.print(i + " ");
}
The output will be: 1 5 4 2 0
- as you wanted.
Remove duplicates in array list react
To convert the array contents.data
to Set, do this:
const setData = new Set(contents.data);
That will remove all the duplicate items.
Then to convert it back, do this:
const uniqueArray = Array.from(setData);
The above will only work if the original array (contents.data
) consisted of primitive values. If it was an array of objects then this will not work as-is and will require some changes.
Remove duplicate Tuples from an ArrayList- Java
Override equals
and hashCode
in your Tuple
class:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Tuple tuple = (Tuple) o;
return first == tuple.first &&
second == tuple.second;
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
Then you can use:
List<Tuple> newList = newNonZeros.stream()
.distinct()
.collect(Collectors.toList());
Or with a for-each loop:
List<Tuple> newList = new ArrayList<>();
for(Tuple tuple : newNonZeros) {
if(!newList.contains(tuple)) {
newList.add(tuple);
}
}
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.
Remove duplicates (both values) - duplicate values from an ArrayList
In Java 8 you can do:
e.removeIf(s -> Collections.frequency(e, s) > 1);
If !Java 8 you can create a HashMap<String, Integer>
. If the String already appears in the map, increment its key by one, otherwise, add it to the map.
For example:
put("123", 1);
Now let's assume that you have "123" again, you should get the count of the key and add one to it:
put("123", get("aaa") + 1);
Now you can easily iterate on the map and create a new array list with keys that their values are < 2.
References:
ArrayList#removeIf
Collections#frequency
HashMap
Related Topics
Java Array with More Than 4Gb Elements
Does Variable = Null Set It for Garbage Collection
Jackson Dynamic Property Names
Favor Composition Over Inheritance
Include External Jar When Running Java -Jar
Information About _Java_Options
Gwt: Timer and Scheduler Classes
Why Is "Out of Range" Not Thrown for 'Substring(Startindex, Endindex)'
Scanner Only Reads File Name and Nothing Else
Count Int Occurrences with Java8
Access "This" from Java Anonymous Class
Create Custom Annotation for Lombok
How to Retrieve a List of Available/Installed Fonts in Android
Parsing a Hexadecimal String to an Integer Throws a Numberformatexception
Differencebetween a Pointer and a Reference Variable in Java