如果那个数减去一半的个数:还不为0,则肯定出现一半以上
- package littlejava;
- public class Array2String
- {
- public static void main(String[]args)
- {
-
- //查找数组中元素个数大于一半的数 ,因为是一半所以只能有一个
- int [] array = {1,1,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,6,25,17,36,3,2,8,9,2};
- int data = _50percent(array);
- System.out.println("数组长度 "+array.length+""+ data);
- }
-
- public static int _50percent(int []array)
- {
- int time=0;
- int index;
- int data = 0;
- for(int i=0;i
- {
- if(time==0)
- {
- data=array[i];
- time=1;
- }
- else
- {
- if(data ==array[i])
- {
- time++;
- }
- else
- {
- time--;
- }
- }
- }
-
- return data;
- }
- }
阅读(892) | 评论(0) | 转发(0) |