Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Maximum occuring element #438

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Beginner Level πŸ“/MaxOccuringElement/MaxOccuringElement.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution{
public:
int maxOccurElement(vector<int>& nums){
sort(nums.begin(),nums.end());
int n=nums.size();
int freq=1, ans=nums[0];
for(int i=1;i<n;i++){
if(nums[i]==nums[i-1]){
freq++;
}else{
freq=1;
ans=nums[i];
}
if(freq>n/2){
ans=nums[i];
return ans;
}
}return ans;
}
};

int main(){
Solution obj;
vector <int> nums={5,4,3,3,5,5,5};
int result= obj.maxOccurElement(nums);
cout<<"The required Element is:"<< result<< endl;
return 0;
}
7 changes: 7 additions & 0 deletions Beginner Level πŸ“/MaxOccuringElement/question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Maximum occuring element in a given array

The maximum occuring element is the element that appears more than [n/2] times. You may assume that the majority element always exists in the array.

Example:
Input: nums=[5,4,3,3,5,5,5]
Output: 5