import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型 */ public int majority_cow (int[] nums) { // write code here Arrays.sort(nums); int value = nums[0] - 1; int count = 0; int i=0; while(i < nums.length) { if (nums[i] != value) { value = nums[i]; count = 0; } else { count++; i++; } if (count > nums.length / 2) { return value; } } return -1; } }
本题主要考察的知识点是数组元素的查找,所用编程语言为java.本题主要利用数组排序将相同元素聚集在一起,再进行查找。也可以使用键值对,键值为数组元素,对应的是数组元素个数