Java implements numbers that appear more than half the times in an array

There is a number in the array that occurs more than half the length of the array, please find the number.

For example, enter an array of length 9 {1, 2, 3, 2. 2, 2. 5, 4, 2}. Because the number 2 appears in the array 5 times, more than half the length of the array, so output 2.

code

public code

    /**
     * Quick sort
     * @param array
     * @param it
     * @param hi
     */
    public static void sort(int[] array,int lo ,int hi){
        if(lo>=hi){
            return ;
        }
        int index=partition(array,lo,hi);
        sort(array,lo,index-1);
        sort(array,index+1,hi);
    }

    public static int partition(int []array,int start,int end){
        // fixed split method
        int key = array[start];
        while(start < end){
            // Scan forward from the second half
            while(end > start && array[end] >= key){
                end--;
            }
            array[start] = array[end];
            // Scan backwards from the first half
            while(start < end && array[start] < key){
                start++;
            }
            array[end]=array[start];
        }
        array[start]=key;
        return end;
    }

Solution one

Because it is an array, and the number of occurrences exceeds half of the length of the array, if the array is sorted, the median of the sorted array should be the number to be found. So first use quick sort to sort the array, and then verify whether the median satisfies the condition.

    private static Integer findMoreThanHalf1(int[] array) {
        if (array == null || array.length == 0) {
            return null;
        }
        int length = array.length;
        int middle = length >> 1;
        // In actual development, who will write their own sorting, just use the ready-made ones
        // Arrays.sort(array)
        sort(array, 0, length - 1);
        int result = array[middle];
        if (!isExist(array, result)) {
            return null;
        }
        return result;
    }

Solution two

The number of occurrences of an element is more than half, that is, the sum of the number of occurrences of other elements is not as many as that of the element, that is, count(target) - count(others) > 0, so compare the two numbers before and after, equal + 1, not equal - 1, when it is 0, update the recorded number. After traversing once, if there is such a number, the counter should be > 0, and the number recorded at this time is the number to be found.

    public static Integer findMoreThanHalf2(int[] array) {
        if (array == null || array.length == 0) {
            return null;
        }
        // Initialize result and count for counting
        int result = array[0];
        int count = 1;
        for (int i = 1; i < array.length; i++) {
            if (array[i] == result) {
                // if equal, the counter +1
                count++;
            } else {
                // if not equal, counter -1
                count--;
            }
            if (count == 0) {
                // If count is 0 at this time, update the result value
                result = array[i];
                count = 1;
            }
        }
        // 1. When the counter is equal to 0, the description does not exist, because it is greater than half of the length of the array, so the case of equal to half is not considered
        // 2. When the counter is greater than 0, verify again whether the number of occurrences of the number is really greater than half the length of the array
        if (count == 0 && !isExist(array, result)) {
            return null;
        }

        return result;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325940134&siteId=291194637