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.
But this will just create the set without duplicates , I want to know which number was duplicate in O(n) time
– ChetanMar 29 '12 at 19:43
Chetan, finding the items in O(n) is possible if the set of possible values is small (think Byte or Short); a BitSet or similar can then be used to store and look up already encountered values in O(1) time. But then again - with such a small value set, doing it in O(n log n) might not be a problem anyway since n is low. (This comment is not applicable to original poster, who needs to do this with String.)
– volleyMay 03 '12 at 12:38
3
@Chetan finding all duplicates from ArrayList in O(n), its important to have correctly defined equals method on objects which you have in the list (no problem for numbers):
`public Set
– Ondrej BozekJun 20 '12 at 12:06
this is great, and it gets even better if you change HashSet to LinkedHashSet
– KevikJul 23 '13 at 11:07
6
A good practice would be to define variables using the interface types `List` and `Set` (instead of implementation types `ArrayList` and `HashSet` as in your example).
– JonikAug 29 '13 at 07:27
35
You can clean this up by using `new HashSet(al)` instead of initializing it to empty and calling `addAll`.
– ashes999Dec 26 '13 at 12:44
1
can I add rules for setting what's duplicate to me? For example: when my `Object` has several values if two of them repeat I consider them as duplicate (other values can be different) and use `Set`?
– jean d'armeAug 18 '15 at 09:32
One reason that I am force to use collection instead of set is due to HTTP API calls. The API returns a POJO of certain objects, I need to act data on it so I really need it not to be duplicated. For some reason, it wasn't part of the API, to return to me a list of non-duplicated data, I cannot wait until it is developed so I must find a way to present a list with no duplicates.
– Neon WargeAug 03 '16 at 07:45
And the OneLiner would be: `myArrayList = new ArrayList(new HashSet(myArrayList));` (But just do it if you really need the ArrayList before AND after this line!)
– r00tandyAug 27 '16 at 11:25
2
@jeand'arme If you use a TreeSet instead of a HashSet, you can define your own Comparator to use, and the TreeSet will consider two items to be duplicates if the Comparators .compare(e1, e2) returns 0. Note that this will destroy the existing order of the arraylist.
– Jarred AllenAug 27 '16 at 21:48
Alternative: `Set set = new HashSet<>(); yourList.removeIf(x -> !set.add(x));` The advantage is that this alternative allows you to decide what to use in the `set.add(…)` expression, for your particular notion of “duplicate”. It’s also independent of the list’s actual elements type. It also retains the order, regardless of whether the set maintains the order or not. Also usable with a `TreeSet` (e.g. with custom comparator) instead of `HashSet`.
– HolgerJun 04 '20 at 07:42
For those, who have problem in understanding
ArrayList arrList3 = new ArrayList(Arrays.asList(1, 23, 1, 3, 3, 2, 3, 2, 12, 67, 23, 12, 34, 9));
Collection set = new HashSet<>(arrList3);
arrList3.clear();
arrList3.addAll(set);
System.out.println(set);
– Jwala KumarApr 10 '22 at 05:30
318
Although converting the ArrayList to a HashSet effectively removes duplicates, if you need to preserve insertion order, I'd rather suggest you to use this variant
// list is some List of Strings
Set<String> s = new LinkedHashSet<>(list);
Then, if you need to get back a List reference, you can use again the conversion constructor.
Does LinkedHashSet make any guarantees as to which of several duplicates are kept from the list? For instance, if position 1, 3, and 5 are duplicates in the original list, can we assume that this process will remove 3 and 5? Or maybe remove 1 and 3? Thanks.
– Matt BriançonMay 01 '11 at 02:20
18
@Matt: yes, it does guarantee that. The [docs](http://download.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html) say: "This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set."
– abahgatMay 02 '11 at 09:00
Very interesting. I have a different situation here. I am not trying to sort String but another object called AwardYearSource. This class has an int attribute called year. So I want to remove duplicates based on the year. i.e if there is year 2010 mentioned more than once, I want to remove that AwardYearSource object. How can I do that?
– WowBowApr 16 '12 at 15:27
@WowBow For example you can define Wrapper object which holds AwardYearSource. And define this Wrapper objects equals method based on AwardYearSources year field. Then you can use Set with these Wrapper objects.
– Ondrej BozekJun 20 '12 at 12:19
@StackFlowed If you don't need to preserve the order of the list you can `addAll` to `new TreeSet(String.CASE_INSENSITIVE_ORDER)`. The first element added will remain in the set so if your list contains "Dog" and "dog" (in that order) the `TreeSet` will contain "Dog". If order must be preserved then before the line in the answer put `list.replaceAll(String::toUpperCase);`.
– PaulNov 03 '17 at 23:28
1
I am getting this error :incompatible types: List cannot be converted to List
– SamirApr 04 '18 at 14:34
This is a simple solution in general but how do you remove the duplicates from an Arraylist of int[]?
– Laser InfiniteJan 23 '20 at 20:13
76
Suppose we have a list of String like:
List<String> strList = new ArrayList<>(5);
// insert up to five items to list.
Then we can remove duplicate elements in multiple ways.
Prior to Java 8
List<String> deDupStringList = new ArrayList<>(new HashSet<>(strList));
Note: If we want to maintain the insertion order then we need to use LinkedHashSet in place of HashSet
Yah, When i typed my previous comments, I was in a impression that `parallel streams` will give better performance always. But it's a myth. I later learned that there are certain scenarios where parallel streams should be used. In this scenario parallel streams will not give any better performance. and yes parallel streams might not give desired results some cases. `List deDupStringList3 = stringList.stream().map(String::toLowerCase).distinct().collect(Collectors.toList());` should be the suitable solution in this case
– DiabloAug 10 '18 at 10:32
53
If you don't want duplicates, use a Set instead of a List. To convert a List to a Set you can use the following code:
// list is some List of Strings
Set<String> s = new HashSet<String>(list);
If really necessary you can use the same construction to convert a Set back into a List.
Similarly at the bottom of the thread, I have given an answer where I am using Set for Custom Object. In a case if anyone have custom object like "Contact" or "Student" can use that answer that works fine for me.
– Muhammad AdilOct 25 '16 at 14:16
The problem comes when you have to specifically access an element. For instance when binding an object to a list item view in Android, you are given its index. So `Set` cannot be used here.
– TheRealChx101Apr 05 '19 at 06:13
Java 8 streams provide a very simple way to remove duplicate elements from a list. Using the distinct method.
If we have a list of cities and we want to remove duplicates from that list it can be done in a single line -
I think this is the best way of removing duplicated in an ArrayList. Definitely recommended. Thank you @Nenad for the answer.
– ByWaleedMar 13 '19 at 09:59
30
Here's a way that doesn't affect your list ordering:
ArrayList l1 = new ArrayList();
ArrayList l2 = new ArrayList();
Iterator iterator = l1.iterator();
while (iterator.hasNext()) {
YourClass o = (YourClass) iterator.next();
if(!l2.contains(o)) l2.add(o);
}
l1 is the original list, and l2 is the list without repeated items
(Make sure YourClass has the equals method according to what you want to stand for equality)
This answer lacks two things: 1) It does not use generics, but raw types (`ArrayList` should be used instead of `ArrayList`) 2) The explicit iterator creating can be avoided by using a `for (T current : l1) { ... }`. Even if you wanted to use an `Iterator` explicitly, `iterador` is misspelled.
– randersDec 07 '15 at 16:22
9
And this implementation runs in quadratic time, compared to the linked hash set implementation running in linear time. (i.e. this takes 10 times longer on a list with 10 elements, 10,000 times longer on a list with 10,000 elements. JDK 6 implementation for [ArrayList.contains](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/util/ArrayList.java#ArrayList.indexOf%28java.lang.Object%29), JDK8 impl is the same.)
– Patrick MJul 11 '16 at 16:09
26
this can solve the problem:
private List<SomeClass> clearListFromDuplicateFirstName(List<SomeClass> list1) {
Map<String, SomeClass> cleanMap = new LinkedHashMap<String, SomeClass>();
for (int i = 0; i < list1.size(); i++) {
cleanMap.put(list1.get(i).getFirstName(), list1.get(i));
}
List<SomeClass> list = new ArrayList<SomeClass>(cleanMap.values());
return list;
}
It is possible to remove duplicates from arraylist without using HashSet or one more arraylist.
Try this code..
ArrayList<String> lst = new ArrayList<String>();
lst.add("ABC");
lst.add("ABC");
lst.add("ABCD");
lst.add("ABCD");
lst.add("ABCE");
System.out.println("Duplicates List "+lst);
Object[] st = lst.toArray();
for (Object s : st) {
if (lst.indexOf(s) != lst.lastIndexOf(s)) {
lst.remove(lst.lastIndexOf(s));
}
}
System.out.println("Distinct List "+lst);
Output is
Duplicates List [ABC, ABC, ABCD, ABCD, ABCE]
Distinct List [ABC, ABCD, ABCE]
@maaartinus Have you tried that code ?. It won't produce any exceptions.Also it is pretty fast. I tried the code before posting.
– CarlJohnOct 18 '13 at 10:35
5
You're right, it doesn't as you iterate the array instead of the list. However, it's slow like hell. Try it with a few millions elements. Compare it to `ImmutableSet.copyOf(lst).toList()`.
– maaartinusOct 18 '13 at 10:49
answers the question I was asked in the interview .. How to remove repeated values from an ArrayList without using Sets. Thanx
– Aniket PaulMay 05 '16 at 09:10
Note that there is an `ImmutableSet.asList()` method, returning an `ImmutableList`, if you need it back as a `List`.
– Andy TurnerOct 27 '17 at 19:25
12
Probably a bit overkill, but I enjoy this kind of isolated problem. :)
This code uses a temporary Set (for the uniqueness check) but removes elements directly inside the original list. Since element removal inside an ArrayList can induce a huge amount of array copying, the remove(int)-method is avoided.
public static <T> void removeDuplicates(ArrayList<T> list) {
int size = list.size();
int out = 0;
{
final Set<T> encountered = new HashSet<T>();
for (int in = 0; in < size; in++) {
final T t = list.get(in);
final boolean first = encountered.add(t);
if (first) {
list.set(out++, t);
}
}
}
while (out < size) {
list.remove(--size);
}
}
While we're at it, here's a version for LinkedList (a lot nicer!):
public static <T> void removeDuplicates(LinkedList<T> list) {
final Set<T> encountered = new HashSet<T>();
for (Iterator<T> iter = list.iterator(); iter.hasNext(); ) {
final T t = iter.next();
final boolean first = encountered.add(t);
if (!first) {
iter.remove();
}
}
}
Use the marker interface to present a unified solution for List:
public static <T> void removeDuplicates(List<T> list) {
if (list instanceof RandomAccess) {
// use first version here
} else {
// use other version here
}
}
EDIT: I guess the generics-stuff doesn't really add any value here.. Oh well. :)
A List will absolutely _work_ as in-parameter for the first method listed. The method is however _optimized_ for use with a random access list such as ArrayList, so if a LinkedList is passed instead you will get poor performance. For example, setting the n:th element in a LinkedList takes O(n) time, whereas setting the n:th element in a random access list (such as ArrayList) takes O(1) time. Again, though, this is probably overkill... If you need this kind of specialized code it will hopefully be in an isolated situation.
– volleyDec 09 '09 at 20:37
This implementation return no element in the list because of the last j--
– neo7Sep 23 '15 at 09:29
1
This implementation work's very fine.there is no issue behind this and for this task i am only use one arraylist.so this answer is completely good.before giving negative feedback you shold also add testcase also so that every one can understand the result.Thanks Manash
– Manash Ranjan DakuaSep 24 '15 at 13:14
5
If you're willing to use a third-party library, you can use the method distinct() in Eclipse Collections (formerly GS Collections).
The advantage of using distinct() instead of converting to a Set and then back to a List is that distinct() preserves the order of the original List, retaining the first occurrence of each element. It's implemented by using both a Set and a List.
MutableSet<T> seenSoFar = UnifiedSet.newSet();
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (seenSoFar.add(item))
{
targetCollection.add(item);
}
}
return targetCollection;
If you cannot convert your original List into an Eclipse Collections type, you can use ListAdapter to get the same API.
If you are using model type List< T>/ArrayList< T> . Hope,it's help you.
Here is my code without using any other data structure like set or hashmap
for (int i = 0; i < Models.size(); i++){
for (int j = i + 1; j < Models.size(); j++) {
if (Models.get(i).getName().equals(Models.get(j).getName())) {
Models.remove(j);
j--;
}
}
}
If you want to preserve your Order then it is best to use LinkedHashSet.
Because if you want to pass this List to an Insert Query by Iterating it, the order would be preserved.
Try this
LinkedHashSet link=new LinkedHashSet();
List listOfValues=new ArrayList();
listOfValues.add(link);
This conversion will be very helpful when you want to return a List but not a Set.
List<String> duplicatList = new ArrayList<String>();
duplicatList = Arrays.asList("AA","BB","CC","DD","DD","EE","AA","FF");
//above AA and DD are duplicate
Set<String> uniqueList = new HashSet<String>(duplicatList);
duplicatList = new ArrayList<String>(uniqueList); //let GC will doing free memory
System.out.println("Removed Duplicate : "+duplicatList);
Perfect - just missing "repeated = false;" in the internal loop after the "if(!repeated) l2.add(c1);" otherwise it return a short list
– kfirAug 23 '21 at 06:25
2
When you are filling the ArrayList, use a condition for each element. For example:
ArrayList< Integer > al = new ArrayList< Integer >();
// fill 1
for ( int i = 0; i <= 5; i++ )
if ( !al.contains( i ) )
al.add( i );
// fill 2
for (int i = 0; i <= 10; i++ )
if ( !al.contains( i ) )
al.add( i );
for( Integer i: al )
{
System.out.print( i + " ");
}
We will get an array {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
As said before, you should use a class implementing the Set interface instead of List to be sure of the unicity of elements. If you have to keep the order of elements, the SortedSet interface can then be used; the TreeSet class implements that interface.
List<String> result = new ArrayList<String>();
Set<String> set = new LinkedHashSet<String>();
String s = "ravi is a good!boy. But ravi is very nasty fellow.";
StringTokenizer st = new StringTokenizer(s, " ,. ,!");
while (st.hasMoreTokens()) {
result.add(st.nextToken());
}
System.out.println(result);
set.addAll(result);
result.clear();
result.addAll(set);
System.out.println(result);
output:
[ravi, is, a, good, boy, But, ravi, is, very, nasty, fellow]
[ravi, is, a, good, boy, But, very, nasty, fellow]
public List<Contact> removeDuplicates(List<Contact> list) {
// Set set1 = new LinkedHashSet(list);
Set set = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (((Contact) o1).getId().equalsIgnoreCase(((Contact) o2).getId()) /*&&
((Contact)o1).getName().equalsIgnoreCase(((Contact)o2).getName())*/) {
return 0;
}
return 1;
}
});
set.addAll(list);
final List newList = new ArrayList(set);
return newList;
}
Why would you post a quadratic solution to a question that already has 2-year-old linear and log-linear solutions, that are also simpler?
– abarnertSep 11 '14 at 07:40
0
The @jonathan-stafford solution is OK. But this don't preserve the list order.
If you want preserve the list order you have to use this:
public static <T> void removeDuplicate(List <T> list) {
Set <T> set = new HashSet <T>();
List <T> newList = new ArrayList <T>();
for (Iterator <T>iter = list.iterator(); iter.hasNext(); ) {
Object element = iter.next();
if (set.add((T) element))
newList.add((T) element);
}
list.clear();
list.addAll(newList);
}
When you use `T element = iter.next();`, you don’t need the unchecked type casts. Or you use `for(T element: list) …` instead of dealing with an `Iterator` manually.
– HolgerJul 04 '19 at 09:58
0
Here is my answer without using any other data structure like set or hashmap etc.
public static <T> ArrayList<T> uniquefy(ArrayList<T> myList) {
ArrayList <T> uniqueArrayList = new ArrayList<T>();
for (int i = 0; i < myList.size(); i++){
if (!uniqueArrayList.contains(myList.get(i))){
uniqueArrayList.add(myList.get(i));
}
}
return uniqueArrayList;
}
“without using any other data structure”, except another `ArrayList`. Which will be very inefficient for larger lists.
– HolgerJul 04 '19 at 09:59
0
Would something like this work better ?
public static void removeDuplicates(ArrayList<String> list) {
Arraylist<Object> ar = new Arraylist<Object>();
Arraylist<Object> tempAR = new Arraylist<Object>();
while (list.size()>0){
ar.add(list(0));
list.removeall(Collections.singleton(list(0)));
}
list.addAll(ar);
}
That should maintain the order and also not be quadratic in run time.
“…and also not be quadratic in run time” *Of course*, this *is* quadratic in run time. It’s even worse than other quadratic solutions.
– HolgerJul 04 '19 at 10:02
0
Time Complexity : O(n) : Without Set
private static void removeDup(ArrayList<String> listWithDuplicateElements) {
System.out.println(" Original Duplicate List :" + listWithDuplicateElements);
List<String> listWithoutDuplicateElements = new ArrayList<>(listWithDuplicateElements.size());
listWithDuplicateElements.stream().forEach(str -> {
if (listWithoutDuplicateElements.indexOf(str) == -1) {
listWithoutDuplicateElements.add(str);
}
});
System.out.println(" Without Duplicate List :" + listWithoutDuplicateElements);
}
This is the right one (if you are concerned about the overhead of HashSet.
public static ArrayList<String> removeDuplicates (ArrayList<String> arrayList){
if (arrayList.isEmpty()) return null; //return what makes sense for your app
Collections.sort(arrayList, String.CASE_INSENSITIVE_ORDER);
//remove duplicates
ArrayList <String> arrayList_mod = new ArrayList<>();
arrayList_mod.add(arrayList.get(0));
for (int i=1; i<arrayList.size(); i++){
if (!arrayList.get(i).equals(arrayList.get(i-1))) arrayList_mod.add(arrayList.get(i));
}
return arrayList_mod;
}
Try `ListIterator i = hashList.listIterator(); i.add("hello"); i.add("hello");`. Alternatively, you can use `hashList.add("a"); hashList.add("b"); hashList.replaceAll(x -> "hello");`. And there might be more ways to counteract this approach in the future. The takeaway is that you shouldn’t try to enforce new contracts via subclassing with classes not designed for that. You will find more about this topic when you search for the “*Prefer composition over inheritance*” OOP rule.
– HolgerJul 04 '19 at 12:41
In Java, List permits ordered access of their elements. They can have duplicates because their lookup key is the position not some hash code, every element can be modified while they remain in the list where as Set represents a collection of unique elements and while elements are in set, they must not be modified.While there is no restriction preventing you from modifying elements in a set, if an element is modified, then it could become forever lost in the set.
public static void main(String[] args) {
List<String> l = new ArrayList<String>();
l.add("A");
l.add("B");
l.add("C");
l.add("A");
System.out.println("Before removing duplicates: ");
for (String s : l) {
System.out.println(s);
}
Set<String> set = new HashSet<String>(l);
List<String> newlist = new ArrayList<String>(set);
System.out.println("after removing duplicates: ");
for (String s : newlist) {
System.out.println(s);
}
}