3

How to remove duplicates from an ArrayList?

I have getCcnptags array as [java,php,c++,c,java,php] which i am getting from bean array, and I am giving hyper link to each array variable, but I want to remove duplicates before adding hyper link to it, does it possible to add any code in my below code to remove duplicates.

for(int k=0;k<name.getCcnptags().size();k++)
    {

    String tag=name.getCcnptags().get(k);
        if(k!=name.getCcnptags().size()-1)
    {
    tag=tag+",";

    }
    %>
    <a href='#'><%=tag%></a>
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2545106
  • 87
  • 1
  • 6
  • Sure, just dump each string into a hashset and then check the hash set before creating an anchor. Your code is pretty poorly formatted. Might want to clean it up. – William Morrison Jul 13 '13 at 05:47
  • possible duplicate of [How do I remove repeated elements from ArrayList?](http://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist) – Raedwald Mar 05 '15 at 21:46

4 Answers4

4

Better use a HashSet. If not possible then you can use a temporary HashSet for this.

ArrayList a= new ArrayList();
HashSet hs = new HashSet();
hs.addAll(a);  // willl not add the duplicate values
a.clear();
a.addAll(hs);  // copy the unique values again to arraylist
stinepike
  • 54,068
  • 14
  • 92
  • 112
3

Use Set interace,A collection that contains no duplicate elements.

From ArrayList create Hashset and use it.

Collection object has a constructor that accept a Collection object to initial the value.

ArrayList yourlist= new ArrayList();
HashSet nodupesSet= new HashSet(yourlist);

Now iterate over nodupesSet

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

I found this on the web. It is much similar to removing elements in a Vector but with change in method names.

import java.util.*;
class RemoveDuplicates
{

    public static void main(String args[])
    {
    ArrayList<String> v=new ArrayList<String>();
    v.add("Gowtham");
    v.add(" Gutha's");
    v.add(" Java");
    v.add("-");
    v.add("demos");
    v.add(".");
    v.add("blogspot");

    // '.' again!
    v.add(".");
    v.add("com ");

    // Gowtham again!
    v.add("gowtham");

    System.out.println("Original");

        for(int i=0;i<v.size();i++)
        {
            System.out.print(v.get(i));

        }

    System.out.println("\nAfter removing 

duplicates");
    removeDuplicates(v);

        for(int i=0;i<v.size();i++)
        {
            System.out.print(v.get(i));
        }

    }


    // Applicable for all types of ArrayLists

    public static void removeDuplicates(ArrayList 

v)
    {
        for(int i=0;i<v.size();i++)
        {
            for(int j=0;j<v.size();j++)
            {
                    if(i!=j)
                    {


if(v.get(i).equals(v.get(j)))
                        {
                        v.remove(j);
                        }
                    }
            }
        }
    }


    /*
        * Specifically applicable for String is 

written for equalIgnoreCase
        * The code..


        public static void 

removeDuplicates(ArrayList<String> v)
        {
            for(int i=0;i<v.size();i++)
            {
                for(int j=0;j<v.size();j++)
                {
                    if(i!=j)
                    {


if(v.get(i).equalsIgnoreCase(v.get(j)))
                        {
                        v.remove(j);
                        }
                    }
            }
        }
    */

}
JavaTechnical
  • 8,846
  • 8
  • 61
  • 97
0

As a side note, if you need the items in your list to remain in their original order, the following approach an be used instead. The set is used to check for duplicate items, and another list is returned.

public list<Object> removeDuplicates(List<Object> list)
{
    List<Object> secondary = new LinkedList<Object>();
    Set<Object> auxillery  = new HashSet<Object>();

    for (Object o : list)
    {
        if (!auxillery.contains(o))
        {
            auxillery.add(o);
            secondary.add(o);
        }
    }

    return secondary;
}

Usage is like this (or similar):

public static final void main(String[] args)
{
    List<Integer> numbers = new LinkedList<Integer>();
    numbers.add(5); numbers.add(6); numbers.add(5); numbers.add(8);

    numbers = removeDuplicates(numbers);
}

Something to remember: if using custom classes, ensure that they override the standard Object.hashCode() method. Otherwise, this approach will not work properly.

Jon C. Hammer
  • 392
  • 4
  • 8