Just a few minor suggestions:
- If you invert the condition on the length, then you can reduce the level of nesting, which is slightly easier to read
- An array will never have a negative length. It's pure paranoia to check for
array.length <= 0
, I suggest to replace witharray.length == 0
- The spacing is not perfect. Use an ideaIDE to reformat the code
- This maybe a matter of taste, but I would rename
countUnique
to simplycount
, and the method fromnumUnique
tocountUnique
Putting it together:
public static int countUnique(int[] array) {
if (array.length == 0) {
return 0;
}
int count = 1;
for (int i = 0; i < array.length - 1; i++) {
if (array[i] != array[i + 1]) {
count++;
}
}
return count;
}