import java.util.*;
public class RemoveDuplicates {
private static Scanner ak;
public static void main(String[] args) {
ak = new Scanner(System.in);
int k=0;
System.out.println("enter the size of the array");
int n=ak.nextInt();
int a[]=new int[n];
for (int i=0;i<n;i++){
System.out.println("enter element "+(i+1));
a[i]=ak.nextInt();
}
Arrays.toString(a);
HashMap<Integer,Integer> h=new HashMap<Integer, Integer>();
for (int i=0;i<n;i++){
if ((h.containsKey(a[i]))){
k=h.get(a[i]);
h.put(a[i],k+1);
}
else{
h.put(a[i], 1);
}
}
System.out.print(h);
Set <Map.Entry<Integer, Integer>> c=h.entrySet();
System.out.print(c);
System.out.println("these are the duplicates removed elements ");
Iterator<Map.Entry<Integer, Integer>> i=c.iterator();
while (i.hasNext()){
if (i.next().getValue()==1)
System.out.println(i.next().getKey());
}
}
}
我编写了一个程序来使用 HashMap 从数组中删除重复项,但我无法打印正确的输出。当我将输入输入为 size=4 并将数组输入输入为 {1,1,2,3} 时,迭代器仅打印“3”,而它应该打印“2,3” 任何帮助将不胜感激