From 80f2e079866ecc8b1f4b88f21a09535d6b6d5356 Mon Sep 17 00:00:00 2001 From: Barun Ojha Date: Sun, 19 Oct 2025 09:00:04 +0530 Subject: [PATCH] Add Majority Element II solution Implement algorithm to find majority elements in an array which appears more than n/3 times. --- Majority_Element_II.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Majority_Element_II.cpp diff --git a/Majority_Element_II.cpp b/Majority_Element_II.cpp new file mode 100644 index 0000000..6bb7a49 --- /dev/null +++ b/Majority_Element_II.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + vector majorityElement(vector& nums) { + int n=nums.size(); + int cnt1=0 , cnt2=0; + int el1=INT_MIN , el2=INT_MIN; + for (int i=0 ; i ls; + int mini = (int)(n/3)+1; + for (int i=0 ; i=mini) ls.push_back(el1); + if (cnt2>=mini) ls.push_back(el2); + return ls; + } +};