Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Majority Element II problem solution

YASH PAL, 31 July 2024

In this Leetcode Majority Element II problem solution, you are given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.

Leetcode Majority Element II problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

import collections
class Solution:
    def majorityElement(self, nums):
        length = len(nums)
        counter = collections.Counter(nums)
        
        if length<3:
            return counter.keys()
        
        result = []
        
        words = counter.most_common(3)
        
        for i in range(0,min(3,len(counter))):
            if words[i][-1]>length/3:
                result.append(words[i][0])
        return result

Problem solution in Java.

public List<Integer> majorityElement(int[] nums) {
    List<Integer> result = new ArrayList<>();
    if(nums.length < 3){
        int n = nums.length;
        for(int i=0; i<n; i++){
            if(!result.contains(nums[i]))
                result.add(nums[i]);
        }
        return result;
    }
        
    Arrays.sort(nums);
    int count=1;
    int gt = nums.length/3;
    for(int i=0; i<nums.length-1; i++){
        if(nums[i] == nums[i+1]){
            count++;
        }
        if(count > 1 && nums[i] != nums[i+1]){ 
            count = 1;
        }
        if(count >= gt+1){
                if(!result.contains(nums[i])){
                    result.add(nums[i]);
                    count = 1;
                }
            }
    }
return result;
}

Problem solution in C++.

vector<int> majorityElement(vector<int>& nums) {
        vector<int> vec;
        int n= floor(nums.size()/3)+1;
        map<int,int> mp;
        map<int,int>::iterator it;    
        for(int i=0;i<nums.size();i++){
            it=mp.find(nums[i]);
            if(it==mp.end())        
                mp.insert(pair<int,int>(nums[i],1));        
            else it->second=mp[nums[i]]+1;        
        } 
        for(it=mp.begin();it!=mp.end();it++){
            if(it->second>=n)        
                vec.push_back(it->first);        
        }
        return vec;   
    }

Problem solution in C.

void add_back(int **ret, int *rS, int num){
    *ret = realloc(*ret, sizeof(int) * (*rS + 1));
    (*ret)[*rS] = num;
    *rS += 1;
    return;
}

int* majorityElement(int* nums, int numsSize, int* returnSize){
    *returnSize = 0;
    if(nums == NULL || numsSize < 1)
        return NULL;
    
   int cand1, cand2, count1, count2;
   cand1 = cand2 = count1 = count2 = 0;
   for (int i = 0; i < numsSize; i++) {
      int num = nums[i];
       if (cand1 == num) {
         count1++;   
       }else if (cand2 == num) {
         count2++;   
       }else if (count1 == 0) {
         cand1 = num;
         count1++;  
       }else if (count2 == 0) {
         cand2 = num;
         count2++;  
       }else {
         count1--;
         count2--;
      }
   }
   
   count1 = count2 = 0;
   for (int i = 0; i < numsSize; i++) {
      int num = nums[i];
      if (cand1 == num)
         count1++;
      else if (cand2 == num)
         count2++;
   }
   int *ret = NULL;

   if(count1 > numsSize/3)
      add_back(&ret, returnSize, cand1);
   
   if(count2 > numsSize/3)
      add_back(&ret, returnSize, cand2); 

   return ret;
}

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes