math - How can I simply check if a set of n numbers are all different? -
i have n integers , need quick logic test see different, , don't want compare every combination find match...any ideas on nice , elegant approach?
i don't care programming language idea in, can convert!
you don't have check every combination commutivity , transitivity; can go down list , check each entry against each entry comes after it. example:
bool areelementsunique( int[] arr ) { for( int i=0; i<arr.length-1; i++ ) { for( int j=i+1; j<arr.length; j++ ) { if( arr[i] == arr[j] ) return false; } } return true; } note inner loop doesn't start beginning, next element (i+1).
Comments
Post a Comment