Skip to main content
deleted 623 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

You're following the same philosophy as the bubble sort, which is very, very, very slow. Have you tried this?:

  • Sort your unordered array with quicksort. Quicksort is much faster than bubble sort (I know, you are not sorting, but the algorithm you follow is almost the same as bubble sort to traverse the array).
  • Then start removing duplicates (repeated values will be next to each other). In a for loop you could have two indices: source and destination. (On each loop you copy source to destination unless they are the same, and increment both by 1). Every time you find a duplicate you increment source (and don't perform the copy).

UPDATE

Your edited code doesn't work for the case arr = new int[] {1, 2, 2, 3} (the last value [3] is lost). Try moving this line: whitelist[destination] = currentValue; to the else clause and also copy it before your while loop. Like this:

whitelist[destination] = currentValue;
while(source < arr.length){
    if(currentValue == arr[source]){
        source++;
    } else {
        currentValue = arr[source];
        destination++;
        source++;
        whitelist[destination] = currentValue;
    }
}

You're following the same philosophy as the bubble sort, which is very, very, very slow. Have you tried this?:

  • Sort your unordered array with quicksort. Quicksort is much faster than bubble sort (I know, you are not sorting, but the algorithm you follow is almost the same as bubble sort to traverse the array).
  • Then start removing duplicates (repeated values will be next to each other). In a for loop you could have two indices: source and destination. (On each loop you copy source to destination unless they are the same, and increment both by 1). Every time you find a duplicate you increment source (and don't perform the copy).

UPDATE

Your edited code doesn't work for the case arr = new int[] {1, 2, 2, 3} (the last value [3] is lost). Try moving this line: whitelist[destination] = currentValue; to the else clause and also copy it before your while loop. Like this:

whitelist[destination] = currentValue;
while(source < arr.length){
    if(currentValue == arr[source]){
        source++;
    } else {
        currentValue = arr[source];
        destination++;
        source++;
        whitelist[destination] = currentValue;
    }
}

You're following the same philosophy as the bubble sort, which is very, very, very slow. Have you tried this?:

  • Sort your unordered array with quicksort. Quicksort is much faster than bubble sort (I know, you are not sorting, but the algorithm you follow is almost the same as bubble sort to traverse the array).
  • Then start removing duplicates (repeated values will be next to each other). In a for loop you could have two indices: source and destination. (On each loop you copy source to destination unless they are the same, and increment both by 1). Every time you find a duplicate you increment source (and don't perform the copy).
edited body
Source Link
morgano
  • 286
  • 4
  • 9

You're following the same philosophy as the bubble sort, which is very, very, very slow. Have you tried this?:

  • Sort your unordered array with quicksort. Quicksort is much faster than bubble sort (I know, you are not sorting, but the algorithm you follow is almost the same as bubble sort to traverse the array).
  • Then start removing duplicates (repeated values will be next to each other). In a for loop you could have two indices: source and destination. (On each loop you copy source to destination unless they are the same, and increment both by 1). Every time you find a duplicate you increment source (and don't perform the copy).

UPDATE

Your edited code doesn't work for the case arr = new int[] {1, 2, 2, 3} (thathe last value [3] is lost). Try moving this line: whitelist[destination] = currentValue; to the else clause and also copy it before your while loop. Like this:

whitelist[destination] = currentValue;
while(source < arr.length){
    if(currentValue == arr[source]){
        source++;
    } else {
        currentValue = arr[source];
        destination++;
        source++;
        whitelist[destination] = currentValue;
    }
}

You're following the same philosophy as the bubble sort, which is very, very, very slow. Have you tried this?:

  • Sort your unordered array with quicksort. Quicksort is much faster than bubble sort (I know, you are not sorting, but the algorithm you follow is almost the same as bubble sort to traverse the array).
  • Then start removing duplicates (repeated values will be next to each other). In a for loop you could have two indices: source and destination. (On each loop you copy source to destination unless they are the same, and increment both by 1). Every time you find a duplicate you increment source (and don't perform the copy).

UPDATE

Your edited code doesn't work for the case arr = new int[] {1, 2, 2, 3} (tha last value [3] is lost). Try moving this line: whitelist[destination] = currentValue; to the else clause and also copy it before your while loop. Like this:

whitelist[destination] = currentValue;
while(source < arr.length){
    if(currentValue == arr[source]){
        source++;
    } else {
        currentValue = arr[source];
        destination++;
        source++;
        whitelist[destination] = currentValue;
    }
}

You're following the same philosophy as the bubble sort, which is very, very, very slow. Have you tried this?:

  • Sort your unordered array with quicksort. Quicksort is much faster than bubble sort (I know, you are not sorting, but the algorithm you follow is almost the same as bubble sort to traverse the array).
  • Then start removing duplicates (repeated values will be next to each other). In a for loop you could have two indices: source and destination. (On each loop you copy source to destination unless they are the same, and increment both by 1). Every time you find a duplicate you increment source (and don't perform the copy).

UPDATE

Your edited code doesn't work for the case arr = new int[] {1, 2, 2, 3} (the last value [3] is lost). Try moving this line: whitelist[destination] = currentValue; to the else clause and also copy it before your while loop. Like this:

whitelist[destination] = currentValue;
while(source < arr.length){
    if(currentValue == arr[source]){
        source++;
    } else {
        currentValue = arr[source];
        destination++;
        source++;
        whitelist[destination] = currentValue;
    }
}
added 623 characters in body
Source Link
morgano
  • 286
  • 4
  • 9

You're following the same philosophy as the bubble sort, which is very, very, very slow. Have you tried this?:

  • Sort your unordered array with quicksort. Quicksort is much faster than bubble sort (I know, you are not sorting, but the algorithm you follow is almost the same as bubble sort to traverse the array).
  • Then start removing duplicates (repeated values will be next to each other). In a for loop you could have two indices: source and destination. (On each loop you copy source to destination unless they are the same, and increment both by 1). Every time you find a duplicate you increment source (and don't perform the copy).

UPDATE

Your edited code doesn't work for the case arr = new int[] {1, 2, 2, 3} (tha last value [3] is lost). Try moving this line: whitelist[destination] = currentValue; to the else clause and also copy it before your while loop. Like this:

whitelist[destination] = currentValue;
while(source < arr.length){
    if(currentValue == arr[source]){
        source++;
    } else {
        currentValue = arr[source];
        destination++;
        source++;
        whitelist[destination] = currentValue;
    }
}

You're following the same philosophy as the bubble sort, which is very, very, very slow. Have you tried this?:

  • Sort your unordered array with quicksort. Quicksort is much faster than bubble sort (I know, you are not sorting, but the algorithm you follow is almost the same as bubble sort to traverse the array).
  • Then start removing duplicates (repeated values will be next to each other). In a for loop you could have two indices: source and destination. (On each loop you copy source to destination unless they are the same, and increment both by 1). Every time you find a duplicate you increment source (and don't perform the copy).

You're following the same philosophy as the bubble sort, which is very, very, very slow. Have you tried this?:

  • Sort your unordered array with quicksort. Quicksort is much faster than bubble sort (I know, you are not sorting, but the algorithm you follow is almost the same as bubble sort to traverse the array).
  • Then start removing duplicates (repeated values will be next to each other). In a for loop you could have two indices: source and destination. (On each loop you copy source to destination unless they are the same, and increment both by 1). Every time you find a duplicate you increment source (and don't perform the copy).

UPDATE

Your edited code doesn't work for the case arr = new int[] {1, 2, 2, 3} (tha last value [3] is lost). Try moving this line: whitelist[destination] = currentValue; to the else clause and also copy it before your while loop. Like this:

whitelist[destination] = currentValue;
while(source < arr.length){
    if(currentValue == arr[source]){
        source++;
    } else {
        currentValue = arr[source];
        destination++;
        source++;
        whitelist[destination] = currentValue;
    }
}
Grammar and formatting fixes
Source Link
Lstor
  • 2.7k
  • 1
  • 21
  • 32
Loading
added 46 characters in body
Source Link
morgano
  • 286
  • 4
  • 9
Loading
Source Link
morgano
  • 286
  • 4
  • 9
Loading