Skip to main content
added 17 characters in body
Source Link
Bill Barry
  • 2.3k
  • 14
  • 18

This would just be a comment but it doesn't all fit...

  1. In compare, you could simply return a-b instead of doing 2 compares.In compare, you could simply return a-b instead of doing 2 compares.
  2. The print can be done with one pass over the array (O(N)) instead of O(N^2):

...

int i = 0;
int value;
while (i < count) {
    value = arr[i];
    i++;
    while (i < count && value == arr[i]) {
        i++;
    }
    cout << value;
}

This would just be a comment but it doesn't all fit...

  1. In compare, you could simply return a-b instead of doing 2 compares.
  2. The print can be done with one pass over the array (O(N)) instead of O(N^2):

...

int i = 0;
int value;
while (i < count) {
    value = arr[i];
    i++;
    while (i < count && value == arr[i]) {
        i++;
    }
    cout << value;
}

This would just be a comment but it doesn't all fit...

  1. In compare, you could simply return a-b instead of doing 2 compares.
  2. The print can be done with one pass over the array (O(N)) instead of O(N^2):

...

int i = 0;
int value;
while (i < count) {
    value = arr[i];
    i++;
    while (i < count && value == arr[i]) {
        i++;
    }
    cout << value;
}
Source Link
Bill Barry
  • 2.3k
  • 14
  • 18

This would just be a comment but it doesn't all fit...

  1. In compare, you could simply return a-b instead of doing 2 compares.
  2. The print can be done with one pass over the array (O(N)) instead of O(N^2):

...

int i = 0;
int value;
while (i < count) {
    value = arr[i];
    i++;
    while (i < count && value == arr[i]) {
        i++;
    }
    cout << value;
}