// Java program for the above approach
import java.util.*;
class GFG{
// Function to find frequency and find gcd
static int find_gcd(int []v, int n)
{
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
// Update the frequency
for (int i = 0; i < n; i++) {
if(mp.containsKey(v[i])){
mp.put(v[i], mp.get(v[i])+1);
}
else{
mp.put(v[i], 1);
}
}
int mini = v[0], maxi = v[0];
for (Map.Entry<Integer,Integer> it : mp.entrySet()) {
mini = mp.get(mini) < it.getValue()
? it.getKey()
: mini;
}
for (Map.Entry<Integer,Integer> it : mp.entrySet()) {
maxi = mp.get(maxi) > it.getValue()
? it.getKey()
: maxi;
}
// Find gcd
int res = __gcd(mini, maxi);
return res;
}
static int __gcd(int a, int b)
{
return b == 0? a:__gcd(b, a % b);
}
// Drive Code
public static void main(String[] args)
{
int [] v = { 2, 2, 4, 4, 5, 5, 6, 6, 6, 6 };
int n = v.length;
System.out.print(find_gcd(v, n) +"\n");
int [] v1 = { 3, 2, 2, 44, 44, 44, 44 };
System.out.print(find_gcd(v1, v1.length) +"\n");
}
}
// This code is contributed by shikhasingrajput