Мне было предложено написать собственную реализацию для удаления дублированных значений в массиве. Вот что я создал. Но после испытаний с 1 000 000 элементов потребовалось очень много времени, чтобы закончить. Есть ли что-то, что я могу сделать, чтобы улучшить алгоритм или удалить какие-либо ошибки?
Мне нужно написать свою собственную реализацию - не использовать Set
, HashSet
и т.д. Или любые другие инструменты, такие как итераторы. Просто массив для удаления дубликатов.
public static int[] removeDuplicates(int[] arr) {
int end = arr.length;
for (int i = 0; i < end; i++) {
for (int j = i + 1; j < end; j++) {
if (arr[i] == arr[j]) {
int shiftLeft = j;
for (int k = j+1; k < end; k++, shiftLeft++) {
arr[shiftLeft] = arr[k];
}
end--;
j--;
}
}
}
int[] whitelist = new int[end];
for(int i = 0; i < end; i++){
whitelist[i] = arr[i];
}
return whitelist;
}