Discover components having not less than K occasions the minimal frequency

    0
    53


    import java.util.*;

      

    class GFG {

        public static void nums_with_least_occurence(int A[], int K)

        {

            

            

            Map<Integer, Integer> hashmp

                = new HashMap<Integer, Integer>();

      

            

            

            for (int i = 0; i < A.size; i++) {

                if (hashmp.containsKey(A[i])) {

                    hashmp.put(A[i],

                               hashmp.get(A[i]) + 1);

                }

                else {

                    hashmp.put(A[i], 1);

                }

            }

      

            

            int mini_occurence = Integer.MAX_VALUE;

            for (Map.Entry<Integer, Integer> entry :

                 hashmp.entrySet()) {

                if (entry.getValue() < mini_occurence)

                    mini_occurence = entry.getValue();

            }

      

            

            

            ArrayList<Integer> ans = new ArrayList<Integer>();

      

            

            

            

            for (Map.Entry<Integer, Integer> entry :

                 hashmp.entrySet()) {

                if (entry.getValue() >= K * mini_occurence)

                    ans.add(entry.getKey());

            }

      

            if (ans.isEmpty())

                System.out.println("No components discovered");

            else {

                for (int i = 0; i < ans.measurement(); i++)

                    System.out.print(ans.get(i) + " ");

            }

            System.out.println();

        }

      

        

        public static void fundamental(String[] args)

        {

            int[] A = { 4, 4, 3, 6, 6, 6, 8, 9 };

            int K = 2;

            nums_with_least_occurence(A, K);

        }

    }

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here