c# - check if the all values of array are different -
c# - check if the all values of array are different -
i have 1 dimensional array this:
int values = new int[5] { 1, 2, 3, 4, 5 };
now checking values different:
bool all_values_are_different() { for(int = 0; < values.length - 1; i++) { search = values[i]; for(int j = + 1; j < 5; i++) { if(search == values[i]) homecoming false; } } homecoming true; }
is there method available in c# check values different in array?
there several ways:
// 1 values.distinct().count() == values.length; // 2 new hashset<int>(values).count == values.length; // 3.1 !values.any(x => values.count(y => x == y) > 1); // 3.2 values.all(x => values.count(y => x == y) == 1);
if result of of these expressions false, means array has duplicates, otherwise elements unique.
c# arrays
Comments
Post a Comment