Showing posts with label How to get distinct values from an array in C#?. Show all posts
Showing posts with label How to get distinct values from an array in C#?. Show all posts

Friday 30 March 2012

How to get distinct values from an array in C#?

How to get distinct values from an array in C#?
[C#]

public int[] GetDistinctValues(int[] arr)
{
List<int> lst = new List<int>();
for (int i = 0; i < arr.Length; i++)
{
if (lst.Contains(arr[i]))
continue;
lst.Add(arr[i]);
}
return lst.ToArray();
}