I have a small list of bytes and I want to test that they\'re all different values. For instance, I have this:
List theList = new List
The similar logic to Distinct
using GroupBy
:
var isUnique = theList.GroupBy(i => i).Count() == theList.Count;
There are many solutions.
And no doubt more beautiful ones with the usage of LINQ as "juergen d" and "Tim Schmelter" mentioned.
But, if you bare "Complexity" and speed, the best solution will be to implement it by yourself. One of the solution will be, to create an array of N size (for byte it's 256). And loop the array, and on every iteration will test the matching number index if the value is 1 if it does, that means i already increment the array index and therefore the array isn't distinct otherwise i will increment the array cell and continue checking.