From 40c8b3cbc0b4683c61744afd492a3eaea04cad8f Mon Sep 17 00:00:00 2001 From: Manasi Wader Date: Sat, 12 Sep 2020 11:05:16 +0530 Subject: [PATCH] find majority of nos in array --- Searching Algorithms/majority_element.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/Searching Algorithms/majority_element.cpp b/Searching Algorithms/majority_element.cpp index d0466e7..392f5b1 100644 --- a/Searching Algorithms/majority_element.cpp +++ b/Searching Algorithms/majority_element.cpp @@ -1,16 +1,22 @@ -//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 using namespace std; -int search(int arr[], int N, int X) +int majorityElement(int a[], int size) { - for(int i=0;in) + return a[i]; } return -1; } @@ -18,10 +24,10 @@ int main() { int n,key; cin>>n; - cin>>key; + int arr[n]; for(int i=0;i>arr[i]; - cout<