I\'m writing a code where I have an int[a] and the method should return the number of unique values. Example: {1} = 0 different values, {3,3,3} = 0 different values, {1,2} = 2 d
Use a set to remove duplicates
public static int differentValuesUnsorted(int[] a) { if (a.length < 2) { return 0; } Set uniques = new HashSet<>(a); return singleUnique.size(); }