By user25778

2008-10-15 08:05:26 8 Comments

I have an ArrayList<String>, and I want to remove repeated strings from it. How can I do this?


@user2868724 2013-10-10 20:24:46

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;

@Tushar Gogna 2017-12-05 07:19:02

I liked this solution better.

@Manikandan K 2019-02-08 11:37:40

Great Dude.........

@stbn 2010-12-28 16:07:10

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

@RAnders00 2015-12-07 16:22:40

This answer lacks two things: 1) It does not use generics, but raw types (ArrayList<T> 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.

@Patrick M 2016-07-11 16:09:24

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, JDK8 impl is the same.)

@Saurabh Gaddelpalliwar 2018-05-09 12:54:46

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++)  {           



@LiNKeR 2019-03-09 01:44:37

If you want your list to automatically ignore duplicates and preserve its order, you could create a HashList(a HashMap embedded List).

public static class HashList<T> extends ArrayList<T>{
        private HashMap <T,T> hashMap;
        public HashList(){
            hashMap=new HashMap<>();

        public boolean add(T t){
                return super.add(t);
            }else return false;

        public boolean addAll(Collection<? extends T> c){
            HashList<T> addup=(HashList<T>)c;
            for(int i=0;i<addup.size();i++){
            }return true;


Usage Example:

HashList<String> hashlist=new HashList<>();
System.out.println(" HashList: "+hashlist);

@saif ali 2019-02-08 15:20:31

Set<String> strSet =;

Is the easiest way to remove your duplicates.

@jonathan-stafford 2008-10-15 08:11:27

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

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

@volley 2009-12-09 20:38:37

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

@Chetan 2012-03-29 19:43:32

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

@volley 2012-05-03 12:38:15

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

@Ondrej Bozek 2012-06-20 12:06:17

@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<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; }

@Kevik 2013-07-23 11:07:49

this is great, and it gets even better if you change HashSet to LinkedHashSet

@Jonik 2013-08-29 07:27:00

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

@ashes999 2013-12-26 12:44:46

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

@jean d'arme 2015-08-18 09:32:06

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?

@Attaullah 2015-10-17 23:18:15

but how to ignore white space in this case????

@maytham-ɯɐɥʇʎɐɯ 2015-11-14 12:25:54

@jonathan-stafford nice and useful peace of code. voteup

@Ninja Coding 2016-07-09 17:52:40

Not working for custom objects like DTOs

@Neon Warge 2016-08-03 07:45:14

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.

@r00tandy 2016-08-27 11:25:26

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

@Jarred Allen 2016-08-27 21:48:25

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

@Giacomo 2018-11-14 15:52:42

what is the time complexity ?

@LiNKeR 2019-03-09 02:00:40

@i_am_zero 2016-05-24 06:17:57

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

Using Guava

List<String> deDupStringList2 = Lists.newArrayList(Sets.newHashSet(strList));

Using Java 8

List<String> deDupStringList3 =;

Note: In case we want to collect the result in a specific list implementation e.g. LinkedList then we can modify the above example as:

List<String> deDupStringList3 =

We can use parallelStream also in the above code but it may not give expected performace benefits. Check this question for more.

@Diablo 2016-06-10 08:13:18

+1 for Java 8 streams. But if case-sensitivity is not required then only Java 8 solution can be easily modified. List<String> deDupStringList3 = stringList.parallelStream().map(String::toLowerCase).distinc‌​t().collect(Collecto‌​rs.toList()); should work.

@Diablo 2018-08-10 10:32:09

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<String> deDupStringList3 =‌​ect(Collectors.toLis‌​t()); should be the suitable solution in this case

@Vinze 2008-10-15 13:48:30

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.

@seekingStillness 2018-07-05 00:14:17

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

@Sameer Shrestha 2018-05-17 02:53:05

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()); -> {
        if (listWithoutDuplicateElements.indexOf(str) == -1) {

    System.out.println(" Without Duplicate List :" + listWithoutDuplicateElements);

@HamidReza 2017-11-30 18:40:58

you can use nested loop in follow :

ArrayList<Class1> l1 = new ArrayList<Class1>();
ArrayList<Class1> l2 = new ArrayList<Class1>();

        Iterator iterator1 = l1.iterator();
        boolean repeated = false;

        while (iterator1.hasNext())
            Class1 c1 = (Class1);
            for (Class1 _c: l2) {
                if(_c.getId() == c1.getId())
                    repeated = true;

@WolfieeifloW 2017-11-30 18:43:02

This thread is years old...

@Gujjula Ramesh Reddy 2017-03-26 06:19:13

This is used for your Custom Objects list

   public List<Contact> removeDuplicates(List<Contact> list) {
    // Set set1 = new LinkedHashSet(list);
    Set set = new TreeSet(new Comparator() {

        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;

    final List newList = new ArrayList(set);
    return newList;

@Nenad Bulatovic 2016-11-04 09:47:10

You can also do it this way, and preserve order:

// delete duplicates (if any) from 'myArrayList'
myArrayList = new ArrayList<String>(new LinkedHashSet<String>(myArrayList));

@ByWaleed 2019-03-13 09:59:38

I think this is the best way of removing duplicated in an ArrayList. Definitely recommended. Thank you @Nenad for the answer.

@Craig P. Motlin 2015-12-10 14:46:58

If you're willing to use a third-party library, you can use the method distinct() in Eclipse Collections (formerly GS Collections).

ListIterable<Integer> integers = FastList.newListWith(1, 3, 1, 2, 2, 1);
    FastList.newListWith(1, 3, 2),

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))
return targetCollection;

If you cannot convert your original List into an Eclipse Collections type, you can use ListAdapter to get the same API.

MutableList<Integer> distinct = ListAdapter.adapt(integers).distinct();

Note: I am a committer for Eclipse Collections.

@hardip 2016-03-26 10:13:30

ArrayList<String> city=new ArrayList<String>();

HashSet<String> hashSet = new HashSet<String>();
Toast.makeText(getActivity(),"" + city.toString(),Toast.LENGTH_SHORT).show();

@Timofey Gorshkov 2011-12-09 22:55:49

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


@Andy Turner 2017-10-27 19:25:15

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

@Ravi Vital 2015-12-02 02:23:21

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


That should maintain the order and also not be quadratic in run time.

@satish 2015-11-17 07:26:39

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>();  
           System.out.println("Before removing duplicates: ");  
           for (String s : l) {  
           Set<String> set = new HashSet<String>(l);  
           List<String> newlist = new ArrayList<String>(set);  
           System.out.println("after removing duplicates: ");  
           for (String s : newlist) {  

for reference , refer this link How to remove duplicates from ArrayList

@neo7 2015-09-23 09:36:02

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

    return uniqueArrayList;

@Manash Ranjan Dakua 2015-09-20 14:41:56

public static void main(String[] args){
    ArrayList<Object> al = new ArrayList<Object>();
    System.out.println("Before Duplicate Remove:"+al);
    for(int i=0;i<al.size();i++){
        for(int j=i+1;j<al.size();j++){
    System.out.println("After Removing duplicate:"+al);

@neo7 2015-09-23 09:29:11

This implementation return no element in the list because of the last j--

@Manash Ranjan Dakua 2015-09-24 13:14:04

This implementation work's very fine.there is no issue behind this and for this task i am only use one 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

@momal 2016-03-09 16:09:54

This saved the day!

@abahgat 2008-10-15 08:18:19

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.

@Matt Briançon 2011-05-01 02:20:23

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.

@abahgat 2011-05-02 09:00:58

@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 2012-04-16 15:27:24

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?

@Ondrej Bozek 2012-06-20 12:19:26

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

@shrini1000 2013-01-11 05:09:20

@WowBow or implement Comparable/Comparator

@infoj 2015-08-12 17:30:38

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 -

 List<String> cityList = new ArrayList<>();

 cityList =;

How to remove duplicate elements from an arraylist

@siva 2015-08-02 09:54:03

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

[ravi, is, a, good, boy, But, ravi, is, very, nasty, fellow]
[ravi, is, a, good, boy, But, very, nasty, fellow]

@sharkbait 2015-07-01 11:54:45

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 =;
   if (set.add((T) element))
      newList.add((T) element);

It's only to complete the answer. Very good!

@sambhu 2015-03-11 12:47:31


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

Note: Definitely, there will be memory overhead.

@M Kaweepatt Churcharoen 2014-12-08 10:57:39

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

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

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

@Thananjayan N 2014-09-11 06:52:56

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

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)

        for(int j=i+1; j<totalSize; j++)
                inputArray[j] = null;
        resultArray[newSize++] = value;

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

@abarnert 2014-09-11 07:40:08

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?

@SparkOn 2014-06-13 12:51:39

    ArrayList<String> list = new ArrayList<String>();
    HashSet<String> unique = new LinkedHashSet<String>();
    HashSet<String> dup = new LinkedHashSet<String>();
    boolean b = false;

    for(Iterator iterator= list.iterator();iterator.hasNext();)
        String value = (String);



@Vitalii Fedorenko 2014-04-20 01:23:18

In Java 8:

List<String> deduped =;

Please note that the hashCode-equals contract for list members should be respected for the filtering to work properly.

@StackFlowed 2016-09-13 20:04:33

How do i do this for case insensitive distinct ?

@Paul 2017-11-03 23:28:14

@StackFlowed If you don't need to preserve the order of the list you can addAll to new TreeSet<String>(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);.

@Samir 2018-04-04 14:34:31

I am getting this error :incompatible types: List<Object> cannot be converted to List<String>

@CarlJohn 2013-10-17 18:38:14

It is possible to remove duplicates from arraylist without using HashSet or one more arraylist.

Try this code..

    ArrayList<String> lst = new ArrayList<String>();

    System.out.println("Duplicates List "+lst);

    Object[] st = lst.toArray();
      for (Object s : st) {
        if (lst.indexOf(s) != lst.lastIndexOf(s)) {

    System.out.println("Distinct List "+lst);

Output is

Duplicates List [ABC, ABC, ABCD, ABCD, ABCE]
Distinct List [ABC, ABCD, ABCE]

@maaartinus 2013-10-18 09:39:41

It's slow and you might get a ConcurrentModificationException.

@CarlJohn 2013-10-18 10:35:28

@maaartinus Have you tried that code ?. It won't produce any exceptions.Also it is pretty fast. I tried the code before posting.

@maaartinus 2013-10-18 10:49:33

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

@Aniket Paul 2016-05-05 09:10:47

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

@Patrick M 2016-07-11 17:32:39

Internally, indexOf iterates the lst using a for loop.

Related Questions

Sponsored Content

60 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

18 Answered Questions

[SOLVED] How to remove an element from a list by index?

  • 2009-03-09 18:16:11
  • Joan Venge
  • 2223930 View
  • 1240 Score
  • 18 Answer
  • Tags:   python list

39 Answered Questions

[SOLVED] How do I efficiently iterate over each entry in a Java Map?

19 Answered Questions

[SOLVED] Accessing the index in 'for' loops?

  • 2009-02-06 22:47:54
  • Joan Venge
  • 1797927 View
  • 3117 Score
  • 19 Answer
  • Tags:   python loops list

32 Answered Questions

[SOLVED] Create ArrayList from array

30 Answered Questions

[SOLVED] Initialization of an ArrayList in one line

31 Answered Questions

[SOLVED] When to use LinkedList over ArrayList in Java?

44 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

33 Answered Questions

[SOLVED] How to split a string in Java

  • 2010-08-14 03:01:53
  • riyana
  • 3566324 View
  • 1481 Score
  • 33 Answer
  • Tags:   java string

9 Answered Questions

[SOLVED] Why is subtracting these two times (in 1927) giving a strange result?

  • 2011-07-27 08:15:58
  • Freewind
  • 602694 View
  • 6462 Score
  • 9 Answer
  • Tags:   java date timezone

Sponsored Content