Dashboard Temp Share Shortlinks Frames API

HTMLify

maxnum_occur.c
Views: 1 | Author: cody
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<stdio.h>
int majorityElement(int* nums, int numsSize) {
    int candidate = nums[0];
    int i, count = 1;
    
    for (i = 1; i < numsSize; i++) {
        if (candidate == nums[i]) {
            count++;
        } else {
            count--;
        }
        
        if (count == 0) {
            candidate = nums[i];
            count = 1;
        }
    }
    
    return candidate;
}

int main() {
    int arr[100], num, j;
    int result;  
    
    printf("Size of array:\n");
    scanf("%d", &num);
    
    for (j = 0; j < num; j++) {
        printf("Element %d value:", j + 1);
        scanf("%d", &arr[j]);
    }
    
    result= majorityElement(arr,num);  
    printf("\nMaximum no:%d",result);
    return 0;
}