Numbers that appear more than half the times in the array (C language + Java)

Posted by wiggly81 on Fri, 17 Dec 2021 20:03:21 +0100

More than half of the numbers appear in the array. This question comes from the sword finger Offer.
A number in the array appears more than half the length of the array. Please find out this number.
There are three solutions

Idea 1:

Because the number we are looking for is more than half of the length, we can sort the array, and then the number will appear in the middle of the array.

C language code

You can use other algorithms such as bubble or fast row. I use the qsort function directly here

//Sorting rules
int cmp_int(const void* s1,const void* s2);
int majorityElement(int* nums, int numsSize){
    qsort(nums,numsSize,sizeof(nums[0]),cmp_int);
    int i = 0;
    return nums[numsSize/2];
}
int cmp_int(const void* s1,const void* s2)
{
    return *(int*)s1 - *(int*)s2;
}

Java code

public int majorityElement(int[] nums) {
        Arrays.sort(nums);
        return nums[nums.length/2];
    }

Idea 2:

We can record the number of occurrences of each number, and then judge whether the largest one is more than half the length of the array. If the C language is a little troublesome, we don't write it, but Java can use HasMap.

Java code

public int majorityElement(int[] nums) {
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int tmp = nums[i];
            if (map.get(tmp) == null) {
                map.put(tmp,1);
            }else{
                map.put(tmp,map.get(tmp)+1);
            }
            if (map.get(tmp) > nums.length/2) {
                return tmp;
            }
        }
        return 0;
    }

Idea 3:

Now we can think about it in another way. If a number operates on half the length of the array, that is, the number of this number is more than the other numbers of the array combined.

Then we can do this. When traversing the array, record two values, one is a number in the array, and the other is the number of times of this number. If the next number is the same as the number currently recorded, the number will be increased by 1. If it is different, the number will be reduced by 1. When the number is 0, We need to save the next number and set the number of times to 1.

If the last number is 1 or greater than or equal to 1, then this number is most likely the number we are looking for.
This idea is similar to taking the offset between two different numbers in the array, because the number of numbers is greater than half of the array, then the last number is the number we are looking for.

C language code

int majorityElement(int* nums, int numsSize){
    
    int result = nums[0];//number
    int count = 1;//Number of words in a number
    int i = 0;
    for (i = 1; i < numsSize; i++) {
        if (count == 0)
        {
            //However, when the number of times is 0, change to the current number and set the number of times to 1
            result = nums[i];
            count = 1;
        }
        else if (nums[i] == result)
        {
            count++;
        }
        else
        {
            count--;
        }
    }
    //Again, judge whether the number of numbers is more than half the length of the array
    count = 0;
    for (i = 0; i < numsSize; i++)
    {
        if (nums[i] == result)
        {
            count++;
        }
    }
    if (count > numsSize/2)
    {
        return result;
    }
    return -1;
}

Java code

public int majorityElement(int[] nums) {
        if (nums == null) {
            return 0;
        }
        //number
        int result = nums[0];
        //Number of occurrences
        int count = 1;
        for (int i = 1; i < nums.length; i++) {
            //The number is offset and replaced with the next number
            if (count == 0) {
                result = nums[i];
                count = 1;
            }else if (nums[i] == result) {
                //Equal on++
                count++;
            }else{
                //Not the same--
                count--;
            }
        }
        //Verify again that the array is more than half the length of the array
        count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == result) {
                count++;
            }
        }
        if (count > nums.length/2) {
            return result;
        }
        return 0;
    }

Title Link A number that appears more than half the times in the array

Topics: Java C Algorithm