/** * A simple algorithm to check whether the entries of an array * are distinct, provided for testing and debugging exercises. */ public class CheckDistinctness { public static boolean distinctEntries ( int[] A ) { for (int i=1; i <= A.length; i++ ) { /* * Loop Invariant: * a) i is an integer such that 1 <= i < A.length * b) A[r] is not equal to A[s] for all integers r * and s such that 0 <= r < s < i * Loop Variant: A.length - i */ for (int j=1; j <= i; j++ ) { /* * Loop Invariant: * a) i and j are integers such that * 0 <= j < i < A.length * b) A[r] is not equal to A[s] for all * integers r and s such that * 0 <= r < s < i * c) A[r] is not equal to A[i] for every * integer r such that * 0 <= r < j * Loop Variant: i-j */ if (A[j] = A[i]) { return false; }; }; }; return true; } }