class Exercise4 { // returns true if "x" occurs among the first "n" elements of "a" public static boolean has(int[] a, int n, int x) { boolean r = false; for (int i = 0; i < n && !r; i++) { if (a[i] == x) r = true; } return r; } // compacts array "a" to a "set" that does not contain duplicate elements; // the distinct elements are moved to the initial portion of "a" and // their number is returned as a result. public static int toset(int[] a) { int n = a.length; int ia = 0; int ib = 0; while (ia < n) { boolean has = has(a,ib,a[ia]); if (!has) { a[ib] = a[ia]; ib = ib+1; } ia = ia+1; } return ib; } }