From 94064f758a7f65cfcb94e568224829b672a2bbaa Mon Sep 17 00:00:00 2001 From: Pragya Patel <146708925+Pragya1001@users.noreply.github.com> Date: Thu, 5 Oct 2023 20:04:28 +0530 Subject: [PATCH] 229_Majority_Element_II.cpp --- .../229_Majority_Element_II.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 LEETCODE SOLUTIONS/229_Majority_Element_II.cpp diff --git a/LEETCODE SOLUTIONS/229_Majority_Element_II.cpp b/LEETCODE SOLUTIONS/229_Majority_Element_II.cpp new file mode 100644 index 0000000..39412df --- /dev/null +++ b/LEETCODE SOLUTIONS/229_Majority_Element_II.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector majorityElement(vector& nums) { + vector ans; + int n=nums.size(); + int count=1; + sort(nums.begin(),nums.end()); + for(int i=1;in/3) + ans.push_back(nums[i-1]); + count=1; + } + } + if(count>n/3) + ans.push_back(nums[n-1]); + return ans; + } +};