查找数组中重复元素的个数

今天看到一道编程题:给定数组String[] a = {"a","b","c","d","a","b","a","c","e"},查找数组中重复元素的个数:预期结果:a:3,b:2.

/**
* 查找数组里的重复元素个数
*/
public static void findRepeatElementCount(String[] arrays){
Map<String,Integer> map = new HashMap<String,Integer>();
for(int i=0;i<arrays.length;i++){
if(map.get(arrays[i])!=null){
map.put(arrays[i],map.get(arrays[i])+1);
}else{
map.put(arrays[i],1);
}
}

//输出值
Set<String> keys = map.keySet();
Iterator<String> its = keys.iterator();
while(its.hasNext()){
String key = its.next();
Integer value = map.get(key);
if(value>1){
System.out.println("元素"+key+" "+value);
}
}
}

上一篇:并发控制--Concurrency control--乐观、悲观及方法


下一篇:1101 Quick Sort (25 分)递推