您的位置:首页 > 其它

查找整数数组中的支配者,即出现次数超过50%的数

2013-08-23 15:20 351 查看
/**
* 查找整数数组中的支配者,即出现次数超过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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐