import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ public int[] findMode (int[] nums) { // write code here Map<Integer, Integer> map = new HashMap<>(); int amount = 0; int value = 0; int[] arr = new int[nums.length]; for (int i = 0; i < nums.length; i++) { map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); int count = map.get(nums[i]); if (count > amount) { value = nums[i]; amount = count; } else if (count == amount) { value = Math.max(value, nums[i]); } arr[i] = value; } return arr; } }
本题考察的知识点主要是数组重复元素的统计,所用编程语言是java。
我们可以用HashMap存储键值对,键值为数组值,相应的对应值为数组值的重复次数,然后我们可以一步一步进行数组元素的次数统计。