BackeasyArraysSwiggy

Dominant Array Value Solution

Problem Statement

Given an integer array nums of size n, identify and return the dominant value. A dominant value is defined as the element that appears strictly more than ⌊n / 2⌋ times in the array. If no such value exists, return 'No dominant value exists'. You may assume that the input array is non-empty.

Example 1
Input
[1, 1, 2, 2, 3, 3, 3, 3, 3]
Output
3

Explanation: Step-by-step: with input [1, 1, 2, 2, 3, 3, 3, 3, 3], we first count the frequency of each element. The frequency of 3 is 5, which is greater than ⌊9 / 2⌋ = 4. Therefore, the dominant value is 3.

Example 2
Input
[1, 1, 1, 2, 2, 3, 3, 3, 3]
Output
No dominant value exists

Explanation: Step-by-step: with input [1, 1, 1, 2, 2, 3, 3, 3, 3], we first count the frequency of each element. The frequency of 1 is 3, the frequency of 2 is 2, and the frequency of 3 is 4. Since 4 is not strictly greater than ⌊9 / 2⌋ = 4, there is no dominant value.

Constraints

  • 1 <= nums.length <= 5 * 10^4
  • -10^9 <= nums[i] <= 10^9
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Dominant Array Value — Problem Statement & Solution Guide

ArraysEasyMoore's Voting
TimeO(n)
|
SpaceO(1)

Problem Description

Given an integer array nums of size n, identify and return the dominant value. A dominant value is defined as the element that appears strictly more than ⌊n / 2⌋ times in the array. If no such value exists, return 'No dominant value exists'. You may assume that the input array is non-empty.

Examples

Example 1

Input

[1, 1, 2, 2, 3, 3, 3, 3, 3]

Output

3

Explanation: Step-by-step: with input [1, 1, 2, 2, 3, 3, 3, 3, 3], we first count the frequency of each element. The frequency of 3 is 5, which is greater than ⌊9 / 2⌋ = 4. Therefore, the dominant value is 3.

Example 2

Input

[1, 1, 1, 2, 2, 3, 3, 3, 3]

Output

No dominant value exists

Explanation: Step-by-step: with input [1, 1, 1, 2, 2, 3, 3, 3, 3], we first count the frequency of each element. The frequency of 1 is 3, the frequency of 2 is 2, and the frequency of 3 is 4. Since 4 is not strictly greater than ⌊9 / 2⌋ = 4, there is no dominant value.

Constraints

  • 1 <= nums.length <= 5 * 10^4
  • -10^9 <= nums[i] <= 10^9

Optimal Approach & Strategy

The optimal approach uses the Boyer-Moore Voting Algorithm to find the dominant element in a single linear pass. By maintaining a candidate variable and a counter that increments for matching elements and decrements for differing ones, the dominant element is guaranteed to be the final candidate. This achieves maximum efficiency with constant extra space.

Brute Force Approach

The brute force approach involves using nested loops to count the frequency of each element in the array. For each element, we scan the rest of the array to count its occurrences, returning the element if its count exceeds n / 2. This method is inefficient as it requires quadratic time.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function findDominant(nums) {
    let candidate = null;
    let count = 0;
    let maxCount = Math.floor(nums.length / 2);
    let maxCandidate = null;
    for (let i = 0; i < nums.length; i++) {
        if (count === 0) {
            candidate = nums[i];
            count = 1;
        } else if (nums[i] === candidate) {
            count++;
        } else {
            count = 1;
            candidate = nums[i];
        }
        if (count > maxCount) {
            maxCandidate = candidate;
            maxCount = count;
        }
    }
    return maxCandidate === null ? 'No dominant value exists' : candidate;
}

Asked in Top Tech Interviews

Swiggy

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.