-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajorityNumber.c
More file actions
35 lines (31 loc) · 861 Bytes
/
majorityNumber.c
File metadata and controls
35 lines (31 loc) · 861 Bytes
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
int majorityElement(int* nums, int numsSize) {
int count=0, countPas=0, element, j=0, pass=0;
int *testedNum=NULL;
for(int i=0; i<numsSize; i++){
for(int k=0; k<j; k++){
if(nums[i]==testedNum[k]){
pass=-1;
}
}
if(pass!=-1){
for(int j=0; j<numsSize; j++){
if(nums[i]==nums[j]){
count++;
}
}
if(count>countPas){
j++;
testedNum=(int *)realloc(testedNum, j*sizeof(int));
testedNum[j-1]=nums[i];
element=nums[i];
}
if(count>numsSize/2){
return element;
}
countPas=count;
count=0;
}
pass=0;
}
return element;
}