Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
}
share|improve this question
 
Is it java code? –  Juned Ahsan Jul 13 at 5:47
 
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 at 5:47

4 Answers

up vote 3 down vote accepted

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
share|improve this answer

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

share|improve this answer

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);
                        }
                    }
            }
        }
    */

}
share|improve this answer
 
Nice !! but using existing useful options is better that again implementing :),Still worthy. –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Jul 13 at 5:56
 
Hmm. Yes! Writing ourselves will be great using existing is smart! What do you say? –  JavaTechnical Jul 13 at 5:57
 
You are true :) we will feel great but code increases like anything,If you start implementing everything on our own :) –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Jul 13 at 5:58
1  
That is why i said using existing is smart! ;) –  JavaTechnical Jul 13 at 5:59

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.