Numbers that appear more than half the times in an array

question:

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}. Since the number 2 appears in the array 5 times, which is more than half the length of the array, 2 is output. Output 0 if not present.

solve:

① The method of solving according to the characteristics of the array has been written before, that is, the number that appears more than half of the number in the array must appear more than the sum of all other numbers, so it can be solved by adding 1 in the same way and subtracting 1 differently. solve.

② O(n) algorithm based on Partition function

A number in the array appears more than half the length of the array. If the array is sorted, the number in the middle of the array after sorting must be the number that appears more than half the length of the array. That is, this number is the statistical median, the n/2th largest number in an array of length n.
This algorithm is inspired by the quicksort algorithm . In the random quicksort algorithm, we first randomly select a number in the array, and then adjust the order of the numbers in the array, so that the numbers smaller than the selected number are placed to the left of it, and the numbers larger than the selected number are placed in it. to the right. If the subscript of the selected number is exactly n/2, then the number is the median of the array. If its subscript is greater than n/2, then the median should be to the left of it, and we can then look up the array in the left part of it. If its subscript is less than n/2, then the median should be to the right of it, and we can then look up the array in the right part of it. This is a typical recursive process.

public class Solution {
    public static int MoreThanHalfNum_Solution(int [] array) {
        return MoreThanHalfNum_Solution(array,array.length);
    }
    public static int MoreThanHalfNum_Solution(int[] arr,int len){
        int mid = len / 2;
        int start = 0;
        int end = len - 1;
        int index = Partition(arr,len,start,end);
        while(index != mid){
            if (index > mid){
                end = index - 1;
                index = Partition(arr,len,start,end);
            }else {
                start = index + 1;
                index = Partition(arr,len,start,end);
            }
        }
        int res = arr[mid];
        int count = 0;
        for (int i = 0;i < len;i ++){
            if (arr[i] == res){
                count ++;
            }
        }
        if (count > len / 2){
            return res;
        }else {
            return 0;
        }
    }
    public static int Partition(int[] arr,int len,int start,int end){
        int pivot = arr[start];
        while (start < end){
            while(start < end && arr[end] >= pivot){
                end --;
            }
            arr[start] = arr[end];
            while(start < end && arr[start] <= pivot){
                start ++;
            }
            arr[end] = arr[start];
        }
        arr[end] = pivot;
        return end;
    }

}

Guess you like

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