I have an ArrayList
with the following strings;
List<String> e = new ArrayList<String>();
e.add("123");
e.add("122");
e.add("125");
e.add("123");
I want to check the list for duplicates and remove them from the list. In this case my list will only have two values, and in this example it would be the values 122 and 125, and the two 123s will go away.
What will be the best way to this? I was thinking of using a Set
, but that will only remove one of the duplicates.
Solution in ArrayList
With the Guava library, using a multiset and streams:
This is pretty, and reasonably fast for large lists (O(n) with a rather large constant factor). But it does not preserve order (
LinkedHashMultiset
can be used if that is desired) and it creates a new list instance.It is also easy to generalise, to instead remove all triplicates for example.
In general the multiset data structure is really useful to keep in ones toolbox.
Something like this (using a Set):
You could use a
HashMap<String, Integer>
.You iterate over the list and if the Hash map does not contain the string, you add it together with a value of 1.
If, on the other hand you already have the string, you simply increment the counter. Thus, the map for your string would look like this:
You would then create a new list where the value for each key is 1.