题解 | #牛的体重统计#
牛的体重统计
https://www.nowcoder.com/practice/15276ab238c9418d852054673379e7bf
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param weightsA int整型一维数组 * @param weightsB int整型一维数组 * @return int整型 */ public int findMode (int[] weightsA, int[] weightsB) { // write code here HashMap<Integer,Integer>s=new HashMap<>(); for(int i=0;i<weightsA.length;i++) s.put(weightsA[i],s.getOrDefault(weightsA[i],0)+1); for(int i=0;i<weightsB.length;i++) s.put(weightsB[i],s.getOrDefault(weightsB[i],0)+1); int r=weightsA.length > 0 ? weightsA[0] : weightsB[0]; for(int k:s.keySet()){ if(s.get(k)>s.get(r)||(s.get(k)==s.get(r)&&k>r)) r=k; } return r; } }
比较基础的hashset