我想从数组中删除重复(重复)元素。我正面临着困难。我尝试了几种方法。请问任何人都可以为我找到解决方案。从Int数组中删除重复(重复)元素
Input: 3,5,6,7,3,5,3,8
Expected Output: 3 5 6 7 8
我的代码:
package com.nanofaroque.DeleteRepElementInArray;
public class DeleteElementRepetativeElement {
public static void main(String[] args) {
// Here is the input Array...
int[] arr = { 3, 5, 6, 7, 3, 5, 3, 8 };
int end = arr.length;// finding the length of the array
for (int i = 0; i < end; i++) {
for (int j = i + 1; j < end; j++) {
if (arr[j] == arr[i]) {
for (int k = j; k < end; k++) {
arr[k] = arr[k + 1];
}
end--;
} else {
j++;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
什么是你期望的输出? – Braj
预期输出:3 5 6 7 8 –
所以你想从3,5,3删除。对? – Braj