Niuke.com Brushing Questions-Numbers that appear more than half the number of times in the array

Problem Description

The number of occurrences of a number in the array exceeds half of the length of the array. Please find out this number.

Input description:
Input an array

Output description:
output more than half of the number of occurrences

Example

Example 1

Enter
[1,2,3,2,2,2,5,4,2]

Output
2

Solutions

analysis

  1. Use the hash method.
  2. Find the number with the most occurrences through the first traversal, and count the number of occurrences of the number in the second traversal

method

  1. Through the hash method, with the help of hashMap to store the number of occurrences of statistics, if a number that meets the conditions is found, return
  2. Find the number with the most occurrences through the first traversal, and count the number of occurrences of the number in the second traversal

Code

// 思路1
public class Solution {
    
      
    public int MoreThanHalfNum_Solution(int[] array) {
    
    

        Map<Integer, Integer> temp = new HashMap<>();
        int count = array.length / 2 + 1;

        for (int i = 0; i < array.length; i++) {
    
    
            if (temp.containsKey(array[i])) {
    
    
                int tempCount = temp.get(array[i]);
                tempCount++;
                if (tempCount >= count) {
    
    
                    return array[i];
                }
                temp.put(array[i], tempCount);
            } else {
    
    
                temp.put(array[i], 1);
            }
        }

        for (Map.Entry<Integer, Integer> entry : temp.entrySet()) {
    
    
            if (entry.getValue() >= count) {
    
    
                return entry.getKey();
            }
        }

        return 0;
    }
}

Time complexity analysis:
O(N): The number of times to traverse the array is N, and the maximum number of times to traverse the map is also N, so the time complexity is O(N)

Space complexity analysis:
O(N): Assuming that there are no repeated numbers in the array, map stores the number of occurrences of all numbers, so the space complexity is O(N)

// 思路2
public class Solution {
    
      
    public int MoreThanHalfNum2(int[] array) {
    
    
        // 找出出现次数最多的数字
        int res = array[0];
        int count = 1;
        for (int i = 1; i < array.length; i++) {
    
    
            if (array[i] == res) {
    
    
                count++;
            } else {
    
    
                count--;
            }
            if (count == 0) {
    
    
                res = array[i];
                count = 1;
            }
        }
        count = 0;
        // 遍历统计出现次数最多的数字的次数
        for (int i = 0; i < array.length; i++) {
    
    
            if (res == array[i]) {
    
    
                count++;
            }
        }
        // 判断次数是否达标
        if (count > array.length / 2) {
    
    
            return res;
        }
        return 0;
    }
}

Time complexity analysis:
O(N): The number of times to traverse the array twice is 2N, so the time complexity is O(N).

Space complexity analysis:
O(1): No extra space is used

If you want to test, you can go directly to the link of Niuke.com to do the test

Numbers that occur more than half the number of times in the array-Niuke.com

Guess you like

Origin blog.csdn.net/qq_35398517/article/details/113643369