Most asked interview question – Check if the array is a dominant array or not
Problem Statement
You are given an array A of length N . An element X is said to be dominant if the frequency of X in A is strictly greater than the frequency of any other element in the A .
For example, if A=[2,1,4,4,4] then 4 is a dominant element since its frequency is higher than the frequency of any other element in A .
Find if there exists any dominant element in A.
Input Format
The first line of input contains a single integer T — the number of test cases.
Then the test cases follow.
The first line of each test case contains an integer N — the size of the array A .
The second line of each test case contains N space-separated integers denoting the array A.
Output Format
For each test case, output YES if there exists any dominant element in A . Otherwise, output NO.
You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical).
Constraints Sample 1:
1 ≤ T ≤ 500
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ N
Sample 1:
Input
4
5
2 2 2 2 2
4
1 2 3 4
4
3 3 2 1
6
1 1 2 2 3 4
Output
YES
NO
YES
N
Source Code – Below source code is using JavaScript Language
function isDominantElementPresent(arr) { var count = {}; arr.forEach(ele=>{ if(count[ele]){ count[ele] +=1; }else{ count[ele] = 1; } }); var maxFrequency = 0; for (var ele in count) { maxFrequency = Math.max(maxFrequency, count[ele]); } var isDominant = Object.values(count).filter(freq => freq === maxFrequency).length === 1; return isDominant; } function main() { const t = parseInt(prompt("Enter the number of test cases:")); for (let i = 0; i < t; i++) { const n = parseInt(prompt("Enter the size of the array:")); const arr = prompt("Enter the array elements separated by space:") .split(" "); if (isDominantElementPresent(arr)) { console.log("YES"); } else { console.log("NO"); } } } main();