我想從數組中刪除重複(重複)元素。我正面臨着困難。我嘗試了幾種方法。請問任何人都可以爲我找到解決方案。從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