class Solution { public boolean hasGroupsSizeX(int[] deck) { //make a frequency map HashMap<Integer,Integer> fmap = new HashMap<>(); for(int val : deck) fmap.put(val,fmap.getOrDefault(val,0)+1); //iterate the frequency map and find GCD(greatest common divisor) if found then //that means they can be divide in GCD groups int ans = 0; for(int val : fmap.keySet()){ ans = gcd(ans,fmap.get(val)); } //if gcd is 1 there is no common divisor except 1 return ans == 1 ? false : true; } public int gcd(int a , int b){ if(b == 0) return a; return gcd(b , a%b); } }