Skip to main content
corrected typo.
Source Link
h.j.k.
  • 19.2k
  • 3
  • 36
  • 93

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 with array.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 simply count, and the method from numUnique to countUnique

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;
}

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 with array.length == 0
  • The spacing is not perfect. Use an idea to reformat the code
  • This maybe a matter of taste, but I would rename countUnique to simply count, and the method from numUnique to countUnique

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;
}

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 with array.length == 0
  • The spacing is not perfect. Use an IDE to reformat the code
  • This maybe a matter of taste, but I would rename countUnique to simply count, and the method from numUnique to countUnique

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;
}
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

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 with array.length == 0
  • The spacing is not perfect. Use an idea to reformat the code
  • This maybe a matter of taste, but I would rename countUnique to simply count, and the method from numUnique to countUnique

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;
}