#include <iostream> using namespace std; int de(int a[], int n) { for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (a[j] == a[i]) { for (int k = j; k < n - 1; k++) a[k] = a[k + 1]; n--; } for ( i = 0; i < n; i++) cout << a[i] << " "; cout << endl; return 0; } int main () { int a[10]; int m = 10; for (int l = 0; l < 10; l++) cin >> a[l]; de(a, m); return 0; }
/** * 编程实现删除数组的重复元素 */ public static int[] delReElement(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); int num = 0; for (int i = 0; i < arr.length; i++) { int e = arr[i]; if (map.get(e) == null) { map.put(e, num); ++num; } else continue; } int[] result = new int[map.size()]; map.forEach((k, v) -> { System.out.println("Key:" + k + " Value:" + v); result[v] = k; }); return result; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String string; while (scanner.hasNext()) { int n = scanner.nextInt(); Set<Integer> numSet = new HashSet<>(); for (int i = 0; i < n; i++) { numSet.add(scanner.nextInt()); } for (Integer integer : numSet) { System.out.println(integer); } } } }
#include<iostream> using namespace std; int shanchu(int data[],int n) {int temp[100]={0};//temp[]数组全部初始化为0 int j; for(int i=0;i<n;i++){ for(j=i+1;j<n;j++){ if(data[i]==data[j]) temp[j]=1;//data[i]=data[j]时,对应的temp[j]赋值为1 }} cout<<"处理后的数组为:"; for(j=0;j<n;j++){ if(temp[j]==0)//temp[j]=1对应的data[j]滤除掉,不输出 cout<<data[j]<<" ";} cout<<endl; return 0; } int main() { int data[100],i=0,n=0; cout<<"please input the array:"; while(cin>>data[i++])n++; shanchu(data,n); return 0; }