-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajorityelementinarray.cpp
More file actions
39 lines (35 loc) · 899 Bytes
/
Copy pathmajorityelementinarray.cpp
File metadata and controls
39 lines (35 loc) · 899 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
36
37
38
39
#include<iostream>
using namespace std;
void checkMajority(int arr[],int size,int candidate){
int c=0;
for(int i=0;i<size;i++)
if(arr[i]==candidate)
c++;
if(c>size/2)
cout<<"\nMajority element is "<<candidate<<" and it occurs "<<c<<" times";
else
cout<<"\nNo majority element ";
}
void findMajority(int arr[],int size){
int maj_index=0;
int count=1;
for(int i=1;i<size-1;i++){
if(arr[maj_index]==arr[i])
count++;
else
count--;
if(count==0){
maj_index=i;
count=1;
}
}
cout<<"\n element occuring max no. of time is "<<arr[maj_index];
checkMajority(arr,size,arr[maj_index]);
}
int main(){
//int arr[]={1,3,2,2,4,2,1,3,3,2,2,2,1,2,2};
int arr[]={1,2,3,4,1,2,3,2,1,3,1,3,1};
int size=sizeof(arr)/sizeof(int);
cout<<"size of the array is "<<size;
findMajority(arr,size);
}