随便一个数组 例如数组一{8.8.8.8.1.5.6.8}  还有一个数组二{11.12.2}  判断数组二中的所有数  再数组一中出现的次数  要是出现次数超过一半  那么就是支配者 如果都没有返回1 

解决方案 »

  1.   

    把数组一的数 放入hashMap里,key为数字,value为在数组一出现的次数,如 key = 8 value=5;
    让后数组二的数字有那些数字为key的值,并且对于的value值大余数组一长度的一半。
      

  2.   


    public class Test15
    { //例如数组一{8.8.8.8.1.5.6.8}  还有一个数组二{8,5,6} 
    //判断数组二中的所有数  再数组一中出现的次数  要是出现次数超过一半
    public static void main(String... args) {
    int[] arr1 = {8,8,8,8,1,5,6,8};
    int[] arr2 = {8,5,6};
    int[] arr3 = new int[3]; for(int i = 0 ; i < arr2.length; i++) {
    int temp = arr2[i];
    for(int j = 0; j < arr1.length;j++) {
    if(temp == arr1[j])
    arr3[i]++;
    }
    } for(int i = 0; i < arr3.length; i++) {
    if(arr3[i] > arr1.length/2)
    System.out.println("支配者: " + arr2[i]);
    }
    }
    }
    支配者: 8
      

  3.   

        public static int getCount() {
            int[] a = {8,8,8,8,1,5,6,8};
            int[] b = {8,12,2};
            int[] c = new int[b.length];
            for(int i = 0; i < b.length; i++) {
                for(int j = 0; j < a.length; j++) {
                    if(b[i] == a[j]) {
                        c[i]++;
                    }
                }
            }
            int count = 0; // 数组二中的所有数在数组一中存在的个数
            for(int k = 0; k < c.length; k++) {
                if(c[k] > a.length / 2) {
                    System.out.println(b[k] + " is dominator");
                }
                if(c[k] != 0) {
                  count++;
                }
                System.out.println("The " + b[k] + " occured time is " + c[k]);
            }
            
            if(count == 0) {
                return 1; // 没有返回1
            }
            return count;
        }
      

  4.   


    import java.util.Arrays;
    public class TestDemo {
        public static void main(String[] argas){
         int[] arrayA={8,8,8,3,1,5,6,8};
         int[] arrayB={8,12,2};
         int sum=0;
         for(int i:arrayB){
         String[] strArray=Arrays.toString(arrayA).split(String.valueOf(i));
         sum=sum+(strArray.length==1?0:strArray.length-1);
         }
         if(sum>=(arrayA.length/2))
         System.out.println("支配者");
         else
         System.out.println("1");
        }
    }