需要在java数组中查找重复整数及其计数
我正在使用下面的代码,但输出不正确 //1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,5,2,1,3,4,6311,1需要在java数组中查找重复整数及其计数,java,Java,我正在使用下面的代码,但输出不正确 //1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,5,2,1,3,4,6311,1 public static void repeat(){ int count[] = new int[]{1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,7,5,2,1,3,4,6,311,1}; int [] done ;
public static void repeat(){
int count[] = new int[]{1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,7,5,2,1,3,4,6,311,1};
int [] done ;
for(int i = 0; i < count.length; i++) {
int a = count[i] ;
int counter=0;
int j=i+1;
//System.out.println(a+"--"+""+j);
for(j=i+1 ; j < count.length; j++){
if (a == count[j]) {
counter=counter+1;
}
System.out.println(a+" is appearing --"+""+counter +" times");
}
}
}
publicstaticvoidrepeat(){
int count[]=新int[]{1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,7,5,2,1,3,4,6311,1};
int[]完成;
for(int i=0;i
尝试创建键类型为Integer且值类型相同的映射。然后遍历count数组-检查map中是否存在给定值-如果不存在,则将其添加为值为1的键。如果确实存在,则只需将该值增加1
在此之后,浏览地图并获取值大于1的所有关键点
public static void repeat(){
int count[] = new int[] {1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,7,5,2,1,3,4,6,311,1};
Map<Integer,Integer> repetitionCount = new HashMap<>();
for(Integer i : count) {
if(repetitionCount.containsKey(i)) {
Integer prevValue = repetitionCount.get(i);
repetitionCount.put(i,prevValue+1);
} else {
repetitionCount.put(i,1);
}
}
repetitionCount.forEach((key,value) -> {
if(value>1) {
System.out.println("Repetition of " + key + " - " + value + " times");
}
}
);
}
publicstaticvoidrepeat(){
int count[]=新int[]{1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,7,5,2,1,3,4,6311,1};
Map repetitionCount=new HashMap();
for(整数i:计数){
if(重复计算容器(i)){
整数prevValue=重复计数get(i);
重复计数put(i,prevValue+1);
}否则{
重复计数(i,1);
}
}
重复计数forEach((键,值)->{
如果(值>1){
System.out.println(“重复“+键+”-“+值+”次数”);
}
}
);
}