1

Is there possible to take unique values from arraylist in c# ?,Actually I have an arraylist containing values = 100,101,102,101,100,102,103,but i want unique values from this such as 100,101,102,103. So what is c# syntax for this to take distinct/unique values from arralist?

I tried

Arraylist Others=new Arraylist(); 
others=TakeanotherValues();
others.Distinct().Toarray();

But error is 'System.Collection.ArrayList Does not copntain defination for Distinct'

user1659510
  • 263
  • 3
  • 6
  • 15

6 Answers6

7

Unless you absolutely have to (e.g. because you're using .NET 1), please don't use ArrayList, it's an old non-generic class. Instead, use List<T> in the System.Collections.Generic namespace, e.g. List<int> or List<object> (the latter is functionally equivalent to an ArrayList since you can add anything to it).
In general, don't use anything directly inside System.Collections unless you're sure of what you're doing; use the collections in System.Collections.Generic instead.

You can use the LINQ method Distinct() on your data, but if you want to use an ArrayList you'll first need to cast it to an IEnumerable<T> using Cast<object>(), like so:

using System.Linq;

// ...

var distinctItems = myList.Cast<object>().Distinct();

This is equivalent to manually creating a set (e.g. HashSet<object>), adding each item of your list to it, and them making a list out of that set, since by definition sets do not keep duplicate items (they don't complain if you insert one though, they just ignore it).

Solal Pirelli
  • 1,199
  • 9
  • 21
5

You can use Linq:

var distinctArraylist = yourArrayList.ToArray().Distinct();
Marco
  • 22,856
  • 9
  • 75
  • 124
1

One possible solution is to loop on the arraylist items, and insert each item in a new Set. So that the Set data structure ensures the uniqueness of items.

using System.IO;
using System;
using System.Collections;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        ArrayList numbers = new ArrayList() {100, 100, 200, 201, 202};
        HashSet<int> uniqueNumbers = new HashSet<int>();
        foreach(int number in numbers) {
            uniqueNumbers.Add(number);
        }
        foreach(int number in uniqueNumbers) {
            Console.WriteLine(number);
        }

    }
}
Rami
  • 7,162
  • 1
  • 22
  • 19
  • This is probably the best non-LINQ solution, since it's what `Distinct()` does internally. (here's the [source](http://referencesource-beta.microsoft.com/#System.Core/System/Linq/Enumerable.cs#4ab583c7d8e84d6d) of the Distinct implementation) – Solal Pirelli Feb 28 '14 at 07:36
  • Thanks @SolalPirelli. Haven't used Linq before since I switched to open source long time ago :) – Rami Feb 28 '14 at 07:38
1

Since ArrayList implements IEnumerable but not IEnumerable<T> you would need to cast it before you can apply the regular LINQ operations:

var distinctArrayList = new ArrayList((ICollection)myArrayList.Cast<int>().Distinct().ToArray());
Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
1
ArrayList list = new ArrayList();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(1);

IEnumerable<int> values = list.Cast<int>().Distinct();

Prints out unique values.

SHM
  • 1,896
  • 19
  • 48
-1

Try this:

(from obj in _arrayList obj).Distinct();
dhrumilap
  • 142
  • 1
  • 17