我仍在努力为此代码获取正确的输入,我需要删除重复项并返回结果。在此之前,我一切正常:
给定数组
[100, 0, 3, 4, 4, 562, 100]
答案应该是
[100, 0, 3, 4, 562]
但我得到的是
[100, 3, 4, 562]
(所以它删除了 0)
有什么办法可以添加到我的代码中,因为我不允许使用任何内置函数、ArrayList、List、Set 等,所以只需实现我自己的解决方案和函数。
public static int[] removeDuplicates(int[] input) {
int[] withoutDubs = new int[input.length];
int pos = 0;
for(Integer element: input) {
if(!checkIfInArray(withoutDubs, element)) {
withoutDubs[pos] = element;
pos++;
}
}
int[] result = new int[pos];
for(int i = 0; i < pos; i++) {
result[i] = withoutDubs[i];
}
return result;
}
public static boolean checkIfInArray(int[] input, int number) {
if(input == null) {
return false;
}
for(Integer num: input) {
if(num == number) {
return true;
}
}
return false;
}