FastPrepElements Occurring More Than n/3 Times

Elements Occurring More Than n/3 Times

Mygate logoMygate● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given an integer array nums of length n, return every distinct value whose frequency is strictly greater than n / 3.

Return qualifying values in increasing numerical order. Frequency counts occurrences of the whole integer value, rather than its individual decimal digits. Return an empty array if no value qualifies.

Function

majorityOverThird(nums: int[]) → int[]

Examples

Example 1

nums = [3,2,3]return = [3]

The value 3 occurs twice, strictly more than 3 / 3 = 1.

Example 2

nums = [1,2,1,2,1,2,3]return = [1,2]

Both 1 and 2 occur three times, more than 7 / 3.

Example 3

nums = [1,2,3,4]return = []

No frequency exceeds 4 / 3.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • For integer frequencies, the threshold is equivalent to frequency > floor(n / 3).

More Mygate problems

See Mygate hiring insights
public int[] majorityOverThird(int[] nums) {
    // write your code here
}
nums[3,2,3]
expected[3]
Checking account…