题解 | #农场牛的标识III#
农场牛的标识III
https://www.nowcoder.com/practice/f8cf74a21aa4440595f007789ea6bd61
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型
*/
public int twoCountNumber (int[] nums) {
// write code here
//使用map计数
HashMap<Integer, Integer> map = new HashMap<>();
for (int key:nums) {
Integer val = map.get(key);
if(val!=null)
map.put(key, val + 1);
else
map.put(key,1);
}
//查找==2的返回
for (int t : map.keySet()) {
if (map.get(t) == 2) {
return t;
}
}
return 0;
}
}
面试高频TOP202 文章被收录于专栏
面试高频TOP202题解
叮咚买菜工作强度 229人发布