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
Here's another approach which is more efficient than Enumerable.Distinct + Enumerable.Count (all the more if the sequence is not a collection type). It uses a HashSet which eliminates duplicates, is very efficient in lookups and has a count-property:
var distinctBytes = new HashSet(theList);
bool allDifferent = distinctBytes.Count == theList.Count;
or another - more subtle and efficient - approach:
var diffChecker = new HashSet();
bool allDifferent = theList.All(diffChecker.Add);
HashSetfalse if the element could not be added since it was already in the HashSet. Enumerable.All stops on the first "false".