Rectangle 27 0

java How do I remove repeated elements from ArrayList?


List<Entity> entities = repository.findByUserId(userId);

Set<Entity> s = new LinkedHashSet<Entity>(entities);
entities.clear();
entities.addAll(s);

This three lines of code can remove the duplicated element from ArrayList or any collection.

Note
Rectangle 27 0

java How do I remove repeated elements from ArrayList?


List<String> al = new ArrayList<>();
// add elements to al, including duplicates
Set<String> hs = new HashSet<>();
hs.addAll(al);
al.clear();
al.addAll(hs);

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).

But this will just create the set without duplicates , I want to know which number was duplicate in O(n) time

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.)

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:

Of course, this destroys the ordering of the elements in the ArrayList.

See also LinkedHashSet, if you wish to retain the order.

You can clean this up by using new HashSet(al) instead of initializing it to empty and calling addAll.

Note
Rectangle 27 0

java How do I remove repeated elements from ArrayList?


public static Object[] removeDuplicate(Object[] inputArray)
{
    long startTime = System.nanoTime();
    int totalSize = inputArray.length;
    Object[] resultArray = new Object[totalSize];
    int newSize = 0;
    for(int i=0; i<totalSize; i++)
    {
        Object value = inputArray[i];
        if(value == null)
        {
            continue;
        }

        for(int j=i+1; j<totalSize; j++)
        {
            if(value.equals(inputArray[j]))
            {
                inputArray[j] = null;
            }
        }
        resultArray[newSize++] = value;
    }

    long endTime = System.nanoTime()-startTime;
    System.out.println("Total Time-B:"+endTime);
    return resultArray;
}

If you want to remove duplicates from ArrayList means find the below logic,

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?

Note
Rectangle 27 0

java How do I remove repeated elements from ArrayList?


List<String> al = new ArrayList<>();
// add elements to al, including duplicates
Set<String> hs = new HashSet<>();
hs.addAll(al);
al.clear();
al.addAll(hs);
public Set<Object> findDuplicates(List<Object> list)       {           Set<Object> items = new HashSet<Object>();           Set<Object> duplicates = new HashSet<Object>();           for (Object item : list) {               if (items.contains(item)) {                   duplicates.add(item);               } else {                   items.add(item);               }           }           return duplicates;       }

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).

But this will just create the set without duplicates , I want to know which number was duplicate in O(n) time

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:

Of course, this destroys the ordering of the elements in the ArrayList.

See also LinkedHashSet, if you wish to retain the order.

You can clean this up by using new HashSet(al) instead of initializing it to empty and calling addAll.

Note
Rectangle 27 0

java How do I remove repeated elements from ArrayList?


// 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.

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:

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.

Note
Rectangle 27 0

java How do I remove repeated elements from ArrayList?


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);
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.

Internally, indexOf iterates the lst using a for loop.

It's slow and you might get a ConcurrentModificationException.

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().

answers the question I was asked in the interview .. How to remove repeated values from an ArrayList without using Sets. Thanx

Note
Rectangle 27 0

java How do I remove repeated elements from ArrayList?


public static <T> void removeDuplicates(List<T> list) {
    if (list instanceof RandomAccess) {
        // use first version here
    } else {
        // use other version here
    }
}

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.

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.

Use the marker interface to present a unified solution for List:

Why use ArrayList in parameter? Why not just List? Will that not work?

Note
Rectangle 27 0

java How do I remove repeated elements from ArrayList?


ImmutableSet.copyOf(list);

Note that there is an ImmutableSet.asList() method, returning an ImmutableList, if you need it back as a List.

There is also ImmutableSet from Guava as an option (here is the documentation):

Note
Rectangle 27 0

java How do I remove repeated elements from ArrayList?


// list is some List of Strings
Set<String> s = new LinkedHashSet<>(list);

@Matt: yes, it does guarantee that. The docs 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."

@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.

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

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.

Then, if you need to get back a List reference, you can use again the conversion constructor.

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?

Note
Rectangle 27 0

java How do I remove repeated elements from ArrayList?


List<String> cityList = new ArrayList<>();
 cityList.add("Delhi");
 cityList.add("Mumbai");
 cityList.add("Bangalore");
 cityList.add("Chennai");
 cityList.add("Kolkata");
 cityList.add("Mumbai");

 cityList = cityList.stream().distinct().collect(Collectors.toList());

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 -

Note