# Dominant Element Codechef Solution

Hello coders, today we are going to solve Dominant Element Codechef Solutions whose Problem Code is DOMINANT2.

### Problem

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 TT — the number of test cases. Then the test cases follow.
• The first line of each test case contains an integer NN — the size of the array AA.
• The second line of each test case contains NN space-separated integers A_1, A_2, \ldots, A_NA1​,A2​,…,AN​ denoting the array AA.

### 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).

• 1 ≤ T ≤ 500
• 1 ≤ N ≤ 1000
• 1 ≤ Ai​ ≤ N

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
NO```

### Explanation:

Test case 1 : 2 is the dominant element.

Test case 2: There does not exist any dominant element.

Test case 3: 3 is the dominant element.

### Dominant Element Codechef Solution in CPP

```#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--) {
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}
vector<int> v;
unordered_map<int, int> m;
for(int i=0; i<n; i++){
m[arr[i]]++;
}
for(auto it: m) v.push_back(it.second);
sort(v.begin(), v.end());
if(v[v.size()-1] > v[v.size()-2]) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}```

### Dominant Element Codechef Solution in Python

```# cook your dish here
z=int(input())
for i in range(z):
n=int(input())
l=list(map(int,input().split()))
h={}
for i in range(len(l)):
if l[i] not in h:
h[l[i]]=1
else:
h[l[i]]+=1
l2=[]
for keys,values in h.items():
l2.append(values)
a=max(l2)
if l2.count(a)>1:
print("NO")
else:
print("YES")```

### Dominant Element Codechef Solution in JAVA

```/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-->0){
int n = sc.nextInt();
int[] input = new int[n];
for(int i=0;i<n;i++){
input[i] = sc.nextInt();
}
Map<Integer,Integer> mp = new HashMap<>();
for(int x: input){
mp.put(x, mp.getOrDefault(x,0)+1);
}
ArrayList<int[]> l = new ArrayList<>();
for(int k : mp.keySet()){
int v = mp.get(k);
}
Collections.sort(l, (o1,o2)->o2[1]-o1[1]);
if(l.size()==1 || l.get(0)[1] > l.get(1)[1]){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}```

Disclaimer: The above Problem (Dominant Element ) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.

Sharing Is Caring