Skip to content

Commit

Permalink
find majority of nos in array
Browse files Browse the repository at this point in the history
  • Loading branch information
WaderManasi committed Sep 12, 2020
1 parent 45676d4 commit 40c8b3c
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions Searching Algorithms/majority_element.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
//Given an integer array and another integer element.
//The task is to find if the given element is present in array or not.
//Given an array A of N elements. Find the majority element in the array. A majority element in an array A of size N is an element that appears more than N/2 times in the array.
//solution:

#include<bits/stdc++.h>
using namespace std;

int search(int arr[], int N, int X)
int majorityElement(int a[], int size)
{
for(int i=0;i<N;i++)
int n=size/2;
int v[100]={0};
int i=0;
for(i=0;i<size;i++)
{
if(arr[i]==X)
return i;
v[a[i]]++;
}
for(i=0;i<size;i++)
{
if(v[a[i]]>n)
return a[i];
}
return -1;
}
int main()
{
int n,key;
cin>>n;
cin>>key;

int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<search(arr,n,key)<<endl;
cout<<majorityElement(arr,n)<<endl;
return 0;
}

0 comments on commit 40c8b3c

Please sign in to comment.