0

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?

7
  • 1
    How are you adding values in ArrayList? Commented Jun 30, 2012 at 7:07
  • Which version of the .NET framework are you using? Commented Jun 30, 2012 at 7:07
  • possible duplicate of Remove duplicates from a List<T> in C# Commented Jun 30, 2012 at 7:08
  • 1
    @AVD this would have be confirmed duplicate once the OP answers my question about the version of the framework. If he is using .NET 1.1 the dupe answer won't be of much help to him. Commented Jun 30, 2012 at 7:09
  • i am adding the value for dataset of query. so remove duplicate value. Commented Jun 30, 2012 at 7:13

6 Answers 6

12

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

3

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

2 Comments

Thanks very elegant. I have a problem with the last element duplicated that's not removed but I solved doing the cycle 2 times.
you can edit answer and submit for other members ; tanx alot
2

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.

Comments

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

1 Comment

Before the above code add the line: Hashtable ht = new Hashtable();
-1

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

5 Comments

i am not wanted sorting but want remove duplicate.
@Mitesh, After the list is sorted, you can remove duplicates by iterating all the elements.
The Distinct method removes the duplicates by itself. If you're to use a linq method, use the right one.
@Falanwe, Isn't the Distinct method only available for string arrays. Is it also available in ArrayList?
it's a linq method: it's available on every type that implements IEnumerable<T>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.