java 求数组中重复元素的个数
2017-06-21 17:03
211 查看
public static void main(String[] args) {
count(new int[]{1, 3, 1, 3,2,4});
}
public static void count(int array[]) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int a : array) {
map.put(a, map.get(a) == null ? 1 : map.get(a) + 1);
}
for(int key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
Arrays.sort(array);
}
用数组元素作为key,然后根据元素key获取,如果null代表没有就存入元素(key) value(个数为1),
如果不为null,就根据元素(key)获取value(个数)加上1
count(new int[]{1, 3, 1, 3,2,4});
}
public static void count(int array[]) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int a : array) {
map.put(a, map.get(a) == null ? 1 : map.get(a) + 1);
}
for(int key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
Arrays.sort(array);
}
用数组元素作为key,然后根据元素key获取,如果null代表没有就存入元素(key) value(个数为1),
如果不为null,就根据元素(key)获取value(个数)加上1
相关文章推荐
- 探讨:java中删除数组中重复元素
- Java 旋转数组查找旋转点和任意元素(元素可重复)
- 一个有趣的问题:怎么在JS的数组中去除重复元素?(JAVA实现吧..)
- Java实现找出数组中重复次数最多的元素以及个数
- Leet Code 26 删除有序数组中重复元素 - Java
- Java中删除数组中重复元素
- [java]一次for循环实现查询有序数组中所有重复的元素
- java 找出n个元素数组中重复次数最多的数(假设出现次数大于n/2)
- java 识别数组中是否具有重复元素
- Java数组去除重复元素
- java去除数组中的重复元素
- Java 旋转数组查找旋转点和任意元素(元素可重复)
- java中删除数组中重复元素方法探讨
- 【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】
- java检测所给数组中是否有重复元素
- 2011-12-04 15:36 JAVA实现位向量给无重复元素的整数数组排序
- java去掉数组中重复的元素
- java去掉数组中重复的元素
- 将一个数组中的重复元素保留一个其他的清零(Java)
- Java - 计蒜客 - 移除数组中的重复元素