andrew9991 于 2016.05.03 17:12 提问

3个回答

w172087242      2016.05.03 17:45

public static void main(String[] args) {
int arr[] = {1,2,43,4,5,5,6,6,6,6,68,2,5,3,4,9,92,3,8,9,94,4,5,6};
Map map = new HashMap();
for (int key : arr) {
Integer v = map.get(key);
if (v == null) {
v = 1;
} else {
v = v.intValue() + 1;
}
map.put(key, v);
}

``````    Collection<Integer> values = map.values();
/** 计算出出现次数最大的值 */
int max = 0;
for (Integer v : values) {
max = Math.max(v, max);
}
/** 找出出现次数为该值的key */
boolean multi = false;
int value = -1;
Iterator<Integer> keys = map.keySet().iterator();
while (keys.hasNext()) {
Integer key = keys.next();
Integer v = map.get(key);
if (v.intValue() == max) {
if (!multi) {
value = key;
multi = true;
} else {
/** 出现多个跳出循环 */
value = -1;
break;
}
}
}

if (value == -1) {
System.out.println("多个数出现次数一致");
} else {
System.out.println("出现最多的值为["+value+"]，出现次数为["+max+"]");
}
}
``````
w172087242 回复little_how: Map<Integer, Integer> map = new HashMap<Integer, Integer>();这句被csdn转义了

w172087242 如果有帮助的话，希望能结帖

w172087242      2016.05.03 17:27

w172087242 给你写段代码，稍等

u011415440   2016.05.03 17:37