Majority Element II

Finding all elements that appear more than ( \left\lfloor \frac{n}{3} \right\rfloor ) times in an array can be efficiently achieved using the Boyer-Moore Voting Algorithm. This problem is a variation that seeks more than one majority element.


  1. Identify Candidates: Since we’re looking for numbers that appear more than ( \frac{n}{3} ) times, there can be at most 2 elements that meet this criterion. We’ll identify two candidates for these majority elements.
  2. Boyer-Moore Voting Algorithm: Use a voting scheme where we keep two counters for two majority candidates. We iterate through the array, and we either increment the counters for the candidates, change the candidates if the counters reach zero, or decrement both counters if the current element is different from both candidates.
  3. Validate Candidates: The above step only finds candidates. We need to validate them by counting their occurrences and checking if they occur more than ( \frac{n}{3} ) times.


class Solution:
    def majorityElement(self, nums: List[int]) -> List[int]:
        # Step 1: Identify Candidates
        candidate1, candidate2, count1, count2 = 0, 1, 0, 0
        for num in nums:
            if num == candidate1:
                count1 += 1
            elif num == candidate2:
                count2 += 1
            elif count1 == 0:
                candidate1, count1 = num, 1
            elif count2 == 0:
                candidate2, count2 = num, 1
                count1 -= 1
                count2 -= 1
        # Step 2: Validate Candidates
        result = []
        for candidate in [candidate1, candidate2]:
            if nums.count(candidate) > len(nums) // 3:
        return result


For nums = [3,2,3], the algorithm identifies 3 as a majority element and returns [3].

Key Takeaways

  • By using the Boyer-Moore Voting Algorithm, we can find the candidates for the majority elements in linear time.
  • The additional pass to validate the candidates ensures that we have the correct majority elements.
  • The time complexity is (O(n)), and the space complexity is (O(1)), where (n) is the length of the array.

Identifying Problem Isomorphism

“Majority Element II” is about finding all elements that appear more than ⌊ n/3 ⌋ times in the array. This uses Boyer-Moore Voting Algorithm with the twist of having two potential candidates for the majority element.

An approximate isomorphic problem to this is “Find All Duplicates in an Array”. In this problem, you are required to find all the elements that appear twice in an array of size 2n, n > 1, 1 ≤ a[i] ≤ n.

In both problems, you need to find specific elements that appear more than a certain number of times. While in “Majority Element II”, it’s more than ⌊ n/3 ⌋ times, in “Find All Duplicates in an Array”, it’s exactly twice. However, the methodology to solve the problems can be different.

“Majority Element II” is more complex than “Find All Duplicates in an Array” due to the need for the Boyer-Moore Voting Algorithm and handling multiple majority candidates, while the other problem is more straightforward and only requires element counting or using the property of the indices. Understanding the simpler problem can lead to grasping the concepts needed for the more complex one.

10 Prerequisite LeetCode Problems

“229. Majority Element II”, involves: array manipulation, counting frequencies, and the use of Moore’s Voting algorithm. Here are 10 problems to prepare:

  1. Array Manipulation:

    • 27. Remove Element: Given an array nums and a value val, remove all instances of that value in-place and return the new length.
    • 88. Merge Sorted Array: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
  2. Counting Frequencies:

    • 169. Majority Element: Given an array nums of size n, find the majority element which is the element that appears more than ⌊n / 2⌋ times.
    • 347. Top K Frequent Elements: Given a non-empty array of integers, return the k most frequent elements.
  3. Moore’s Voting Algorithm:

  4. Combinations of above concepts:

    • 283. Move Zeroes: Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.
    • 448. Find All Numbers Disappeared in an Array: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array.
    • 287. Find the Duplicate Number: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
    • 75. Sort Colors: Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

Clarification Questions

Here are some clarification questions that could be asked about this problem of finding frequent elements in an array:

  • Is the input array guaranteed to have at least one element that exceeds the threshold?

  • Can the array contain negative numbers or only positive numbers?

  • Are there any constraints on the range of values besides -109 to 109?

  • Does the array contain only unique elements or can duplicates be present?

  • Should the returned elements be unique even if multiple occurrences pass the threshold?

  • Is the order of the returned elements significant?

  • Can we modify or sort the input array?

  • Should we return an empty array if no elements pass the threshold?

  • For edge cases like an empty array input, what should be returned?

  • Can we leverage additional memory (more than O(1) space) if it results in faster computation?

  • Are there specific time or space complexity requirements we should aim for?

Asking targeted clarifying questions ensures we fully understand edge cases, constraints, requirements, tradeoffs, and complexity goals before implementing a solution.

Problem Analysis and Key Insights

Here are some key insights gained from analyzing this frequent elements problem statement:

  • It’s fundamentally a filtering problem - we want to extract elements meeting a criteria. This suggests using some selection algorithm.

  • The frequency threshold is proportional to array length. So we need a way to count occurrences in relation to size.

  • The constraints imply a simple brute force approach may not be efficient enough. More optimized solutions needed.

  • Returned elements shouldn’t have duplicates themselves even if multiple occurrences pass threshold.

  • The follow up tells us outright to focus on linear time and constant space solutions - a major hint about efficiency requirements.

  • Examples cover small arrays which are useful but we need to ensure solutions scale well per constraints.

  • No constraints on input value ranges or duplicates are given - so we cannot make assumptions there.

In summary, recognizing this as a frequency-based filtering problem with strict efficiency constraints points clearly towards more optimized selection algorithms, likely using counting and thresholding based on the array length and element frequencies.

Problem Boundary

Analyzing the problem statement, the scope of this problem appears to be:

  • Data structures - An integer array is given, requiring array manipulation

  • Algorithms - Frequency counting and filtering algorithms needed

  • Time complexity - Must achieve O(N) linear time complexity

  • Space complexity - Must use O(1) constant additional space

  • Input values - Integers from -109 to 109, no other constraints specified

  • Problem domain - Falls in the domains of algorithms, arrays, frequency analysis

  • Programming - Imperative programming with arrays, counters, conditionals

  • Problem variations - Extensions to other data structures or frequency thresholds appear out of scope

  • Edge cases - Standard edge cases around empty/singleton arrays need handling

So in summary, the scope is focused on leveraging an algorithmic approach for frequency counting and selection in arrays to achieve efficient linear time and constant space solutions. Significant extensions or variations appear outside the scope of core requirements.

Here are some ways we can delineate the boundaries and scope of this frequent elements problem:


  • Integer array of unspecified values and duplicates


  • Integers from -109 to 109
  • Array length from 1 to 50,000


  • Array containing elements over threshold
  • No duplicate elements in output

Frequency Threshold:

  • Based on array length, not predefined value


  • Must achieve O(N) time and O(1) space


  • Can modify and sort input array

Edge Cases:

  • Empty or single element array
  • No elements passing threshold

Out of Scope:

  • Data structures other than arrays
  • Different frequency threshold formulas
  • Returning statistic summaries instead of elements

So in essence, the problem has reasonably well-defined inputs, outputs, efficiency goals and clearly articulated edge cases, giving a clear sense of boundaries.

Problem Classification

This is a problem in the domain of algorithms and data structures. It involves analyzing an array of integers to determine which elements pass a frequency threshold.

The key “what” components are:

  • An integer array nums
  • A frequency threshold based on the array length
  • Finding elements that exceed the threshold

Based on these aspects, we can further classify this problem as:

  • Array/List problem since it operates on array data
  • Frequency analysis problem because it calculates frequency
  • Filtering problem since it filters out elements below a threshold
  • Integer constraints problem due to the integer array domain

Additionally, the follow up specifies:

  • Linear time complexity requirement
  • Constant space complexity requirement

So in summary, this is an algorithmic array frequency filtering problem with added time and space complexity constraints. The core is filtering an array based on element frequencies exceeding a length-based threshold.

Distilling the Problem to Its Core Elements

This problem involves filtering an array based on element frequencies, so the core concepts are frequency analysis and selection.

I would describe it simply as: given a list of numbers, find the values that appear many times in the list - more than one-third the length of the list.

The core problem is to identify elements that exceed a frequency threshold based on the array length. We can simplify it to finding frequent elements.

The key components are:

  • The input array of integers
  • Calculating each element’s frequency
  • Comparing frequencies to length-based threshold
  • Filtering out elements below the threshold

The minimal operations needed are:

  • Counting each element’s frequency
  • Comparing frequencies to threshold
  • Filtering elements based on threshold

So in essence, this leverages frequency analysis and selection to filter an array for elements that pass a length-dependent frequency threshold. The key is counting occurrences and filtering based on a data-dependent threshold.

Visual Model of the Problem

Here are some ways we could visualize the problem statement for finding frequent elements in an array:

  • Show the array values in a grid with frequency counters above certain values that exceed the threshold.

  • Use a bar or pie chart to illustrate the frequencies of all elements, highlighting the ones above threshold.

  • Draw a diagram with two sections - accepted elements vs filtered out elements based on the frequency threshold test.

  • Animate traversing the array, incrementing counters for each element, then filtering into buckets based on meeting threshold.

  • Visualize allowed memory usage - drawing it all being used up except a tiny sliver for the required O(1) space.

  • Timeline showing elements being added to array over time, with counters updating and being threshold tested.

  • Statistics plot showing distribution of frequencies and the threshold line overlayed.

  • Visual metaphor like crowded subway cars with most elements standing but some having many open seats.

The goal is to use engaging visuals like plots, animations and metaphors to illustrate the logical constraints and conditions with visual perspectives.

Problem Restatement

Here’s how I would paraphrase the problem statement in my own words:

We want to filter an array of integers to find elements that appear very frequently - specifically, more than one third of the array length.

For example, if the array has length 6, we would find elements that appear more than 6 / 3 = 2 times.

The input is an array of integers that can contain duplicates. The output is the filtered set of elements that meet the frequency threshold test.

Our goal is to write an efficient algorithm that:

  • Counts the frequency of each element
  • Identifies elements whose frequency exceeds the one third length threshold
  • Returns those frequent elements without duplicates

And we need to aim for O(N) linear time complexity and O(1) constant additional space complexity.

Let me know if I’m misinterpreting any aspects of the problem based on this paraphrasing. I want to ensure I understand the requirements clearly before moving forward. Please feel free to correct or clarify any part of my interpretation.

Abstract Representation of the Problem

Here’s one way we could formulate an abstract representation of this problem:

Let’s define:

S = A set of n elements

F(e) = The frequency or number of occurrences of element e in S

T = A predefined threshold

We want to:

  1. Calculate F(e) for all elements e in S

  2. Filter S to a subset S’ that contains only elements e where F(e) > T

  3. Return the set S'

So in plain terms, we have:

  • A set S
  • A frequency function F
  • A threshold T

We want to:

  1. Determine F for all elements
  2. Filter to elements where F exceeds T
  3. Return filtered set

By removing details about the specific array, integers, and frequency formula, we can describe this problem at a high level as:

Given a set, threshold, and function defining frequency of elements, filter the set to elements whose frequency exceeds the threshold.


Here are some key technical concepts relevant to this frequent elements problem:

Frequency - The number of occurrences of a particular element within a data set. Critical for the frequency thresholding.

Linear time - An algorithm with O(n) runtime, meaning it scales linearly with the size of the input. Required per the constraints.

Constant space - An algorithm using O(1) additional memory, meaning it has a fixed memory overhead regardless of input size. Also required.

Hash table - A data structure that supports efficient insertion and frequency counting in O(1) time. Can help optimize frequency tracking.

Boyer-Moore majority vote - A linear time algorithm for finding the majority element over a threshold by counting occurrences with relative votes. Relevant technique.

Prefix sum - Running totals of element counts used to determine frequencies in constant time. Enables efficient frequency queries.

In summary, core concepts are the frequency metric, linear and constant complexity goals, and relevant techniques like hashing, voting, and prefix sums that can enable optimally efficient solutions within the problem constraints.

Problem Simplification and Explanation

Let’s break this down into simpler concepts and a metaphor:

Key concepts:

  • Array - A list of numbers that may contain duplicates

  • Frequency - How often a number appears in the list

  • Threshold - A minimum frequency value

  • Filter - Extracting certain elements based on a criteria

The goal is to filter out numbers that don’t meet the minimum frequency threshold.


Imagine the array is a bus full of passengers where each number is a different passenger.

The frequency is how often a passenger type appears - maybe 2 parents, 10 kids, and 50 commuters.

The goal is to let only the “frequent” passenger types disembark - those above a minimum threshold like 20.

So we count the instances of each passenger, and only let types off the bus that meet the minimum frequency criteria.

In simpler terms, we want to count occurrences of numbers in a list, and then filter out those below a certain frequency threshold.


Based on the problem statement, here are some specific characteristics we can leverage:

  • Integer array limits us to counting frequencies of discrete values rather than continuous values. Discrete counting can be simpler.

  • Large input size means we need highly efficient algorithms. Can leverage algorithms like Boyer-Moore majority vote that operate in linear time.

  • Length-based threshold depends directly on input size. We can use this relationship if we track size to calculate threshold in constant time.

  • Output must be element values, not frequency counts or statistics. This allows optimized frequency tracking internally as long as we output just values.

  • No output order requirements are specified. This allows flexibility like using a hash table for unordered storage.

  • Modifications like sorting are allowed. We can potentially sort to group duplicates and optimize counting.

  • Strict space limit prevents using external data structures. Limits options but allows creativity like modifying input array.

These characteristics allow targeting efficient frequency tracking schemes and leveraging the length-threshold relationship and flexible output format to optimize within constraints.

Analyzing the constraints provided in the problem statement gives us a few key insights:

  • Linear time requirement - This suggests algorithms like Boyer-Moore voting which operate in O(N) time. Standard brute force is likely insufficient.

  • Constant space requirement - We cannot use external data structures and must operate in-place. Limits options but allows repurposing input array.

  • Integer values - Discrete integer range allows optimizations like hashing or index tracking frequency counts directly in the input array.

  • Length-based threshold - The threshold depends directly on the input size. We can calculate it in O(1) if we track length.

  • Output values only - We can focus optimizations on frequency tracking rather than formatting if output is just values.

  • Unordered return - This allows using data structures like hash tables which have efficient O(1) frequency queries but unordered output.

  • Array modifications allowed - Sorting or overwriting input array enables optimizations like grouping duplicates.

The key takeaways are the strict efficiency needs push us past brute force solutions while the length-dependent threshold and flexible output allow certain optimizations like voting algorithms, hash maps, array sorting/modification to achieve the required efficiency.

Case Analysis

Here are some additional test cases covering a wide range of inputs:

  1. Empty array

Input: [] Output: [] Reasoning: Validates handling empty input.

  1. One element

Input: [1] Output: [1] Reasoning: Ensure threshold of 1 handled for size 1 input.

  1. No matches

Input: [1, 2, 3]
Output: [] Reasoning: Handles case where no elements meet threshold.

  1. All match

Input: [2, 2, 2, 2, 2] Output: [2] Reasoning: Checks duplicate handling when all values match.

  1. Two matches

Input: [1, 2, 3, 2, 2, 2] Output: [2] Reasoning: Validates extracting only elements meeting criteria.

  1. Boundary values

Input: [10^9, 10^9, -10^9, -10^9] Output: [10^9, -10^9] Reasoning: Checks boundary value handling.


  • Empty input
  • Singleton input
  • No matches
  • All match
  • Multiple matches
  • Boundary values

Edge Cases:

  • Empty input
  • One element input
  • No matches

Covering a spectrum of cases helps validate correctness and ensure robustness across a diverse set of possible inputs and outputs.

Here are some ideas for visualizing test cases for finding frequent array elements:

  • Frequency plots - Plot each element’s frequency on a bar or pie chart. Shade or label the ones above threshold.

  • Animations - Traverse array visually and animate incrementing counters, then filtering elements based on threshold.

  • Grid diagrams - Show array as grid with row frequencies and highlight rows meeting criteria.

  • Set diagrams - Visualize input, frequencies, and output as a Venn diagram or Euler diagram.

  • Decision trees - Use a tree structure to model the decisions and branching logic.

  • Memory diagrams - Illustrate memory usage and constant additional space constraint.

  • Table view - Show test case inputs and expected outputs in a table, checking them off as tests pass.

  • Edge case emphasis - Use colors, annotations, etc. to emphasize edge case behavior.

  • Fascinating numbers - Transform abstract large input values into more engaging numbers and visuals.

Leveraging a variety of visual representations can help complement logical test cases with more intuitive diagrams, plots, animations and metaphors.

Analyzing these test cases provides a few key insights:

  • Need to handle empty and single element edge cases correctly to avoid exceptions

  • Should return empty result when no elements meet threshold, not throw errors

  • Must account for all values matching threshold, not just one

  • Duplicates should be filtered in output to avoid redundant elements

  • Boundary values like max/min integers need validation to catch edge behaviors

  • Varied input distributions should be tested like sparse matches and dense matches

  • Visual depictions reveal off-by-one type errors in logic and corner cases

  • Real-world datasets provide protection against unpredictable distributions

  • Meticulous specification of expected I/O for each case flushes out assumptions

The main takeaways are ensuring the solution is watertight across standard and edge cases by thoroughly specifying expected behavior for diverse examples, visually inspecting logic, and testing against real-world data to catch subtle bugs. Robustness requires defense in depth.

Identification of Applicable Theoretical Concepts

Some mathematical and algorithmic concepts that could help optimize this frequent elements problem include:

  • Boyer-Moore majority vote - This linear time algorithm counts element frequencies with relative votes to identify a majority element. We can adapt it to find all elements above a threshold.

  • Counting sort - Counting sort could arrange elements in order of frequency in linear time, after which we could filter.

  • Bucket sort - We could bucket elements into frequency buckets and extract buckets above threshold.

  • Hash table - Use a hash table to track frequencies in O(1) time, then output keys above threshold.

  • Prefix sum - Calculate prefix sums of frequencies, then use binary search to identify elements above threshold quickly.

  • Sampling - Use reservoir sampling to get an estimated frequency distribution on large datasets, then filter exact frequencies.

  • Divide and conquer - Split the array, find frequent elements in subarrays, then combine the results.

Applying these algorithms, data structures, and techniques allows performing the core frequency counting and filtering steps optimally. The key is mapping to known efficient approaches.

Simple Explanation

Here’s how I would explain this frequent elements problem in simple non-technical terms:

Let’s say you survey people about their favorite fruit - apples, oranges, bananas, etc. You gather results from 100 people total.

Now you want to find the most commonly liked fruits - the ones liked by more than a third of all people surveyed.

So you tally how many people like each fruit - maybe 50 people like apples, 20 like oranges, 10 bananas.

Given there were 100 people total, you set the threshold as 100 / 3 = 33 since a third of 100 is 33.

Any fruits with more than 33 votes make the cut. So in this example, only apples pass the threshold.

The fruits with more than 33 votes are considered the “frequent favorite fruits” since a relatively large fraction of people liked them.

So in simple terms, given a list of options with votes or occurrences, we take the total count, divide by 3 to get a threshold, and find the options exceeding that threshold based on their frequency.

Problem Breakdown and Solution Methodology

Here is how I would approach solving this problem of finding frequent elements in an array:

  1. Count the frequencies of each element using a hash map to store the counts.

Loop through the array incrementing the counter for each element, taking O(N) time with a hash map.

  1. Calculate the threshold based on the array length.

The threshold is length / 3. Simply divide the length by 3.

  1. Filter the hash map to elements meeting threshold.

Check each key in the hash map, retaining ones with value exceeding threshold.

  1. Return the keys of the filtered hash map.

This gives us the frequent elements.

For example on [1, 2, 2, 3, 3, 3], the steps would be:

  1. Counting gives frequencies of 1:1, 2:2, 3:3

  2. Length is 6, threshold is 6/3 = 2

  3. Filtering keeps only keys with frequency > 2, so keep 3:3

  4. Return [3]

This leverages a hash map to efficiently count frequencies in linear time, then applies a simple threshold filter to extract frequent elements also in linear time. Changing the length or values won’t significantly change the approach.

Inference of Problem-Solving Approach from the Problem Statement

Here are key terms and how they guide the approach:

  • Array - The input as an array informs iterating over elements and accessing by index.

  • Frequency - Counting frequency of elements guides using a mapping structure to tally occurrences.

  • Threshold - Having a threshold indicates applying a filter based on the counts.

  • Linear time - The O(N) constraint pushes us to optimized algorithms like hashing versus brute force.

  • Constant space - The O(1) space limit prevents external data structures, informing in-place optimization.

  • Integer values - Integers allow direct hashing and counting versus continuous values needing approximations.

  • Length dependency - Basing threshold on length allows calculating it efficiently in one pass without needing to store entire counts.

  • Output values - Outputting just values focuses logic on efficient frequency tracking and filtering rather than concerns like sorting the output.

So key terms like frequency, threshold, linear time, constant space, and integers guide choices around data structures, algorithms, and optimizations to achieve efficient frequency-based filtering within constraints.

We can use tables and diagrams to visualize some key properties and aspects of this frequent elements problem:

  • Sample array - Show the input array values in a table to clarify the raw input data.

  • Frequency table - Table with elements and their corresponding frequencies calculated.

  • Threshold line - Visualize the threshold graphically as a horizontal line on a frequency plot.

  • Histogram - Bar chart of element frequencies with threshold line overlayed.

  • Logic flow - Diagram the steps involved like counting, thresholding, filtering as a flow chart.

  • Hash table - Show hash table storing frequencies and crossed out items filtered.

  • Set diagram - Visualize input, frequencies, and output as a Venn diagram.

  • Time complexity - Use a table mapping input size N to time order O(N) based on linear efficiency need.

The goal is using visual artifacts like tables and plots to make logical components more concrete - input data, relationships between elements and frequencies, the thresholding process, intermediate structures, and big O time complexity.

How did you infer from the problem statement that this problem can be solved using ?

Simple Explanation of the Proof

I’m having trouble understanding the proof of this algorithm. Could you explain it in a way that’s easy to understand?

Stepwise Refinement

Here is one way we could break this down into more refined steps:

High-level approach:

  1. Count frequency of each element
  2. Determine threshold
  3. Filter by threshold

Further refined steps:

  1. Traverse array incrementing counters
  2. Calculate threshold as length / 3
  3. Check each element’s count against threshold
  4. Output elements above threshold

Independent sub-problems:

  • Counting frequencies
  • Calculating threshold
  • Filtering by threshold
  • Outputting final elements

Repeat patterns:

  • Array traversal
  • Threshold comparisons
  • Hash map usage for counting

We can break the key stages like frequency counting, thresholding, and filtering into independent sub-problems. We also leverage repeated patterns like array traversal and hash map based counting.

Solution Approach and Analysis

Here is how I would approach solving this frequent elements problem:

  1. Use a hash map to count the frequency of each element as we traverse the array.

This allows incrementing frequencies in O(1) time.

  1. Calculate the threshold as the array length divided by 3.

Simple constant time operation.

  1. Check each key in the hash map, adding elements to output that exceed threshold.

Filtering based on comparing counts to the threshold.

  1. Return the output array.

Contains the frequent elements.

For example on input [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]:

  1. Frequencies map to 1:1, 2:2, 3:3, 4:4

  2. Length is 10, threshold is 10/3 = 3

  3. 4 has count greater than threshold.

  4. Return [4].

This leverages a hash map to enable counting in linear time, then applies the length-based threshold to filter elements in linear time. Changing length or values doesn’t significantly affect the approach.

Identify Invariant

The key invariant in this frequent elements problem is:

At each iteration, the frequency map data structure correctly stores the up-to-date frequency count for each element encountered so far.

In other words, the frequency tracking data structure (like a hash map) always maintains updated frequencies reflecting the array elements seen up to the current iteration.

This is established by:

  • Initializing the frequency map empty before processing the array.

  • Incrementing the counter for each element as it is processed.

  • Not modifying counts except when that element is encountered.

This invariant is important because:

  • It allows constant time queries of an element’s current frequency at any iteration.

  • The final frequency counts can be trusted to accurately reflect the complete array.

  • The threshold filtering logic can thus rely on accurate frequency data.

So this invariant ensures the frequency tracking structure is kept consistent and up-to-date during execution, enabling efficient and reliable threshold filtering using the final computed frequencies.

Identify Loop Invariant

What is the loop invariant in this problem?

Thought Process

Here is how I would approach solving this type of frequent elements problem:

The main cues in the problem statement are:

  • Array input indicates iterating over elements
  • Frequency threshold implies counting occurrences
  • Constraints suggest optimized algorithms over brute force

This points to an approach leveraging efficient frequency counting and filtering.

The thought process involves:

  1. Set up a frequency map to count occurrences of each element
  2. Iterate array incrementing counters in map
  3. Calculate threshold based on input size
  4. Check counts against threshold
  5. Output elements above threshold

The key insights are using a map for efficient O(1) frequency tracking, relating threshold to input size, and applying a threshold filter on the counts.

Here is Python code implementing this approach:

from collections import defaultdict

def frequent_elements(arr, k):

  freq = defaultdict(int)
  for elem in arr:
    freq[elem] += 1

  threshold = len(arr) // k
  results = []
  for key,value in freq.items():
    if value > threshold:
  return results

The core concepts are frequency counting, thresholding based on size, and filtering - common patterns for selection problems requiring efficiency.

Here is one approach to solve this problem using a hashmap in Python:

from collections import defaultdict

class Solution:
    def majorityElement(self, nums: List[int]) -> List[int]:
        # Hashmap to count frequency of each number
        count = defaultdict(int)
        for num in nums:
            count[num] += 1
        # Calculate n/3 threshold
        n = len(nums)
        threshold = n // 3
        # List to store majority elements 
        res = []
        # Find all numbers with frequency > threshold
        for num, freq in count.items():
            if freq > threshold:
        return res

The key steps are:

  1. Use a hashmap to count the frequency of each number efficiently in O(N) time.

  2. Calculate the n/3 threshold based on array length.

  3. Check each number’s frequency against the threshold.

  4. Return all numbers whose frequency exceeds the threshold.

The hashmap allows counting frequencies in linear time. Comparing to the threshold filters numbers based on the problem criteria.

This overall approach runs in O(N) time and O(N) space complexity due to the hashmap.

Establishing Preconditions and Postconditions

Here is an analysis of the majorityElement method:


  • nums: The input array of integers
  • Its type is List[int]
  • It represents the data set to analyze


  • nums cannot be null
  • nums.length > 0
  • nums contains integers

Method Functionality:

  • Counts the frequency of each element in nums
  • Filters for elements that appear more than n/3 times
  • Returns a list of the majority elements


  • nums remains unchanged
  • Returns a List of integers
  • No side effects

Error Handling

  • If nums is null, raises a NullPointerException
  • If nums is empty, returns empty list
  • If nums contains non-integers, skips those values

So in summary, this performs frequency counting, filtering, and returning majority elements on the input array, without side effects on the parameters or program state.

Problem Decomposition

Here’s how I would break down this majority element problem:

  1. Problem Understanding: The goal is to find all elements in an array that appear more than n/3 times, where n is array length.

  2. Initial Breakdown: Major parts are:

  • Count element frequencies
  • Determine threshold
  • Filter elements by threshold
  1. Subproblem Refinement:
    Key tasks are:
  • Track frequencies in hash table
  • Calculate threshold from length
  • Check frequency vs threshold
  • Collect elements above threshold
  1. Task Identification: Counting frequencies and comparing to threshold are repeated tasks that could be generalized.

  2. Task Abstraction: We can define abstract “count_frequency” and “apply_threshold” tasks.

  3. Method Naming: count_frequencies, calculate_threshold, filter_by_threshold, collect_results

  4. Subproblem Interactions: First count frequencies, then calculate threshold based on that length. Use threshold to filter frequencies, then collect results.

Breaking the problem down systematically into sub-tasks and their interactions helps clarify the key components and structure before implementing the solution.

From Brute Force to Optimal Solution

Here is one approach for optimizing a brute force solution:

Brute force:

  • Nested loops checking every pair for frequency n/3
  • O(N^2) time complexity

The brute force solution is inefficient, checking every single pair unnecessarily.

Optimization 1 - Count frequencies:

  • Loop once tracking frequencies in hash map
  • O(N) time complexity

Counting frequencies with a hash map improves runtime by avoiding redundant pairwise checks.

Optimization 2 - Threshold cells:

  • Mark cells exceeding threshold while counting
  • Still O(N) time

We optimize further by detecting threshold directly during counting.

Optimization 3 - Sampling:

  • Randomly sample array to estimate distribution
  • then take full pass for exact counts
  • Improves expected time

Sampling provides probabilistic speedup for large arrays by avoiding a full pass.

Optimizing from the brute force nested approach to tracking frequencies then comparing to thresholds reduces the complexity from O(N^2) to O(N). The optimizations build incrementally, each improving upon the prior solution.

Code Explanation and Design Decisions

Here’s how I would analyze this majority element code:

  1. The inputs are the array nums to analyze and a hashmap freq to track frequencies.

  2. The main loop iterates over nums counting occurrences of each element in freq. This builds up the frequency data needed.

  3. The conditions check if the frequency exceeds the computed threshold. This filters based on problem criteria.

  4. freq is updated by incrementing the counters. This maintains frequency metadata during iteration.

  5. The invariant is that freq always holds accurate up-to-date frequencies at each iteration. This ensures correctness.

  6. The final output contains elements exceeding the threshold. This satisfies the goal of filtering for majority elements.

In summary, the core logic revolves around tracking comprehensive frequency data in the hashmap, updating it accurately throughout iteration, then leveraging it to filter elements based on crossing the frequency threshold. The metadata supports efficient frequency-based filtering.

Coding Constructs

Here’s how I would analyze the high-level logic of this majority element code:

  1. The overall strategy is frequency-based filtering - tracking occurrences to filter by a frequency threshold.

  2. Broadly, the code counts how often each element appears and selects ones that appear frequently.

  3. Main logical constructs are a hashmap for counting, iteration, conditionals for thresholding, and aggregation into output.

  4. The algorithm iterates through the input, counts frequencies using a hashmap, filters for elements above a threshold, and collects those elements.

  5. Key steps are iterating data, incrementing counters, comparing counters to threshold, and collecting majority elements. This implements the frequency filtering strategy.

  6. General patterns are iteration, aggregation, hashmaps, filtering via conditionals, and thresholding.

In summary, the core logic utilizes a frequency counting and threshold filtering strategy built on iteration, aggregation, hashmaps, and conditionals. The code encapsulates a common algorithm pattern for statistics-based filtering.

Language Agnostic Coding Drills

Here’s how I would break down this majority element code into concepts:

Basic Concepts:

  • Variables to store values
  • Arithmetic like incrementing counters
  • Conditionals (if statements)
  • Iteration over data

Intermediate Concepts:

  • Hash tables for key-value storage
  • Nested conditionals
  • Aggregating values into collections

Advanced Concepts:

  • Thresholding based on dynamic inputs
  • Algorithm analysis and optimization
  • Space/time tradeoffs (hashmap vs array)

The process would involve:

  1. Implementing core language variables, arithmetic, conditionals, iteration
  2. Using variables and iteration to aggregate values
  3. Applying aggregation to build hash table frequency counters
  4. Using conditionals and thresholding to filter aggregated data
  5. Optimizing data structures and tradeoffs to improve efficiency

Each concept builds on the fundamentals to incrementally construct the solution. The drills target specific coding techniques for aggregating, thresholding, and optimizing to filter array data based on frequency.

Targeted Drills in Python

Here are some example Python coding drills for key concepts:

Basic Concepts:

# Variables
count = 0

# Arithmetic
count += 1 

# Conditionals
if count > 10:
  print("Limit reached")
# Iteration
for i in range(10):

Intermediate Concepts:

# Hash tables
counts = {}
counts["A"] = 5

# Nested conditionals
if x > 10 and y < 5:
  print("Both conditions met")
# Aggregation
total = 0
for num in list:
  total += num

Problem-specific Concepts:

# Threshold checking 
if count > length // k:
  print("Threshold exceeded")
# Frequency counting
for letter in string:
  frequencies[letter] += 1

We would combine these in order:

  1. Basic language constructs
  2. Intermediate aggregations and conditionals
  3. Problem-specific frequency counting and thresholding

Building up incrementally from base language concepts to problem-specific techniques enables a systematic progression from fundamentals to final solution.


Similar Problems

Can you suggest 10 problems from LeetCode that require similar problem-solving strategies or use similar underlying concepts as the problem we’ve just solved? These problems can be from any domain or topic, but they should involve similar steps or techniques in the solution process. Also, please briefly explain why you consider each of these problems to be related to our original problem.