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();
}
[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();
}
No comments:
Post a Comment