Mitesh Machhi
Mitesh Machhi

Reputation: 203

How to remove duplicate value from ARRAYLIST

I have one arraylist which contains values from a different database, but it stores some duplicate values so, I want to remove duplicate values and store only unique value in Array List.

How can this be done?

Upvotes: 0

Views: 27888

Answers (6)

R.Akhlaghi
R.Akhlaghi

Reputation: 760

you can using This code when work with an ArrayList

ArrayList arrayList;
//Add some Members :)
arrayList.Add("ali");
arrayList.Add("hadi");
arrayList.Add("ali");

//Remove duplicates from array
  for (int i = 0; i < arrayList.Count; i++)
    {
       for (int j = i + 1; j < arrayList.Count ; j++)
           if (arrayList[i].ToString() == arrayList[j].ToString())
                 arrayList.Remove(arrayList[j]);
    }

Upvotes: 3

sanjay
sanjay

Reputation: 11

        Hashtable ht = new Hashtable();
        foreach (string item in originalArray){
            //set a key in the hashtable for our arraylist value - leaving the hashtable value empty
            ht[item] = null;
        }

    //now grab the keys from that hashtable into another arraylist
    ArrayList distincArray = new ArrayList(ht.Keys);

Upvotes: 1

The Original Android
The Original Android

Reputation: 6215

Let's try another method. Instead removing duplicates, avoid adding any duplicates. This might be more efficient in your environment. Here's a sample code:

ArrayList<String> myList = new ArrayList<string>();
foreach (string aString in myList)
{
    if (!myList.Contains( aString ))
    {
        myList.Add(aString);
    }
}

Upvotes: 12

Falanwe
Falanwe

Reputation: 4744

If you can, you should use a HashSet, or any other set class. It is much more efficient for this kind of operation. The main default of the HashSet is that the ordering of the element is not guaranteed to remain the same as your original list (wich may or may not be a problem, depending on your specifications).

Otherwise, if you need to keep the ordering, but only need the duplicates removed when you enumerate through your values, you can use the Distinct method from linq. Just be careful and don't run this query and copy the result everytime you modify your arraylist as it risks impacting your performances.

Upvotes: 2

The Original Android
The Original Android

Reputation: 6215

If you must use ArrayList, use the Sort method. Here's a good link @ Sort Method of ArrayList. After the list is sorted, then use an algorithm to iterate/compare all your elements and remove the duplicates.

Have fun,

Tommy Kwee

Upvotes: -1

brianestey
brianestey

Reputation: 8302

You can replace your ArrayList with a HashSet. From the documentation:

The HashSet<T> class provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.

If it's absolutely necessary to use an ArrayList, you could use some Linq to remove duplicates with the Distinct command.

var distinctItems = arrayList.Distinct()

Upvotes: 4

Related Questions