/**
* 查找整数数组中的支配者,即出现次数超过50%的数
*/
package test;
import java.util.ArrayList;
import java.util.Arrays;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {2,4,8,1,5,10,3,3,3,3,3,3,3,3,3,-5,0};
System.out.println("it is "+Judge(a));
}
public static int Judge(int[] ints){
Arrays.sort(ints);
System.out.println(ints.toString());//不能查看数组元素
int count = 1;//每个数的出现次数
for(int i=0; i<ints.length-1; i++){
if(ints[i]==ints[i+1]){
count++;
if((double)count/ints.length>0.5){
return ints[i];
}
}else{
count = 1;
}
}
return -1;
}
}
查找整数数组中的支配者,即出现次数超过50%的数
最新推荐文章于 2023-03-19 19:09:52 发布