Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the unique values of an array in .net?

Say I've got this array: MyArray(0)="aaa" MyArray(1)="bbb" MyArray(2)="aaa"

Is there a .net function which can give me the unique values? I would like something like this as an output of the function: OutputArray(0)="aaa" OutputArray(1)="bbb"

like image 386
Anthony Avatar asked Sep 17 '08 13:09

Anthony


People also ask

How do you find the unique list of values?

Using Python's import numpy, the unique elements in the array are also obtained. In the first step convert the list to x=numpy. array(list) and then use numpy. unique(x) function to get the unique values from the list.

How do you find the number of unique elements in an array in C++?

Calculate the length of an array using the length() function that will return an integer value as per the elements in an array. Call the sort function and pass the array and the size of an array as a parameter. Take a temporary variable that will store the count of distinct elements. Print the result.

How do you check if all numbers in an array are unique?

All(x => values. Count(y => x == y) == 1); If the result of any of these expressions is false, that means your array has duplicates, otherwise all elements are unique. Save this answer.


1 Answers

Assuming you have .Net 3.5/LINQ:

string[] OutputArray = MyArray.Distinct().ToArray();
like image 142
James Curran Avatar answered Sep 28 '22 14:09

James Curran