如何在重复数组中保留零值?
How to keep zero values in an array of duplicates?
我仍在努力为这段代码获取正确的输入,我需要删除重复项并 return 结果。我把一切都做好了,直到:
给定数组[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;
}
withoutDubs
第一次实例化时默认用0填充
因此checkIfInArray(withoutDubs, 0)
returnstrue
即使0
在数组中只出现一次
您可以将索引传递给 checkIfInArray
,这样它就不会搜索所有 withoutDubs
数组。它应该只检查索引 0
到 pos - 1
.
public static boolean checkIfInArray(int[] input, int last, int number) {
if(input == null) {
return false;
}
for(int i = 0; i < last; i++) {
if(input[i] == number) {
return true;
}
}
return false;
}
并将方法调用从
更改为
checkIfInArray(withoutDubs, element)
至
checkIfInArray(withoutDubs, pos, element)
既然你不能使用任何内置函数,那么你可以自己实现它们:一种来自contains
和grow
的方法ArrayList
。由于 int[]
数组默认包含 zeros,您可以使用 Integer[]
数组代替。
public static void main(String[] args) {
int[] arr = {100, 0, 3, 4, 4, 562, 100};
Integer[] noDuplicates = new Integer[0];
for (int i = 0, j = 0; i < arr.length; i++) {
if (!contains(noDuplicates, arr[i])) {
noDuplicates = grow(noDuplicates);
noDuplicates[j++] = arr[i];
}
}
// output: [100, 0, 3, 4, 562]
System.out.println(Arrays.toString(noDuplicates));
}
static boolean contains(Integer[] arr, int val) {
for (Integer el : arr) {
if (el != null && el == val) {
return true;
}
}
return false;
}
static Integer[] grow(Integer[] arr) {
Integer[] target = new Integer[arr.length + 1];
System.arraycopy(arr, 0, target, 0, arr.length);
return target;
}
我仍在努力为这段代码获取正确的输入,我需要删除重复项并 return 结果。我把一切都做好了,直到:
给定数组
[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;
}
withoutDubs
第一次实例化时默认用0填充
因此checkIfInArray(withoutDubs, 0)
returnstrue
即使0
在数组中只出现一次
您可以将索引传递给 checkIfInArray
,这样它就不会搜索所有 withoutDubs
数组。它应该只检查索引 0
到 pos - 1
.
public static boolean checkIfInArray(int[] input, int last, int number) {
if(input == null) {
return false;
}
for(int i = 0; i < last; i++) {
if(input[i] == number) {
return true;
}
}
return false;
}
并将方法调用从
更改为checkIfInArray(withoutDubs, element)
至
checkIfInArray(withoutDubs, pos, element)
既然你不能使用任何内置函数,那么你可以自己实现它们:一种来自contains
和grow
的方法ArrayList
。由于 int[]
数组默认包含 zeros,您可以使用 Integer[]
数组代替。
public static void main(String[] args) {
int[] arr = {100, 0, 3, 4, 4, 562, 100};
Integer[] noDuplicates = new Integer[0];
for (int i = 0, j = 0; i < arr.length; i++) {
if (!contains(noDuplicates, arr[i])) {
noDuplicates = grow(noDuplicates);
noDuplicates[j++] = arr[i];
}
}
// output: [100, 0, 3, 4, 562]
System.out.println(Arrays.toString(noDuplicates));
}
static boolean contains(Integer[] arr, int val) {
for (Integer el : arr) {
if (el != null && el == val) {
return true;
}
}
return false;
}
static Integer[] grow(Integer[] arr) {
Integer[] target = new Integer[arr.length + 1];
System.arraycopy(arr, 0, target, 0, arr.length);
return target;
}