You have an array of random integers that may be repeating. Find the first N least frequently occurring integers in this array. Your solution method may return these N integers in any order inside the output array.
HINTS
The following hints are just one example of how you may solve this problem. You may choose to use other approaches using other data structures but, if you do, please make sure you are importing the standard Java libraries from java.util.*.
You may consider building a HashMap that stores each integer's value and its corresponding frequency first. This hash map is your dictionary of key-value pairs that give you each integer's number of occurrences in the input.
Then, use that HashMap to build a heap of size N. Will this heap be a min or max heap?
At the end, your new heap should only have N integers
You will get these N integers from the heap into your output array to return; these N integers can be in any order in your output array.
Lastly: sometimes, you may have a test case where all of the integers in input occur exactly for the same number of times. For example: [1,1,2,2,3,3,4,4] and N=2. In this case, your output array of size 2 can be any combination of these integers.
STARTER CODE
import java.util.*;
public class Solution2 {
public static void main(String[] args) {
Solution sol = new Solution();
// Your solution methods will be tested with random input; the following is just
// an example. The actual test cases will be different.
int[] output = sol.getN(new int[]{1,2,3,4,5,6}, 3);
// You may use this approach to print your output to console to validate
System.out.println(Arrays.toString(output)); // combination of any 3 input integers
// [2,3,6], [1,2,3], [4,5,6], etc. // If you wish to repeat with a different test case, you may as follows:
output = sol.getN(new int[]{1,1,1,2,2,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,6}, 3);
System.out.println(Arrays.toString(output)); // [6,5,2] or them 3 in any order
}
} class Solution {
// YOU MAY ADD ANY ADDITIONAL METHODS, VARIABLES, ETC., HERE
/**
* PURPOSE: * PARAMETERS: * RETURN VALUES:
*/ public int[] getN(int[] input, int N) {
// YOUR CODE HERE }
}
EXAMPLES
input: [1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2], 1
output: [2]
Note: N = 1, the least occurring integer in the input is 2.
input: [1], 1
output: [1]
Note: N = 1, the least occurring integer in the input is 1.
input: [11,12,1,2,10,100], 3
output: any combination of 3 integers in the input array
Note: N = 3, and every integer occurs once in the input array. So, any combination of 3 integers from the input array can be the answer.
input: [11,12,1,2,10,100], 3
output: any combination of 3 integers in the input array
Note: N = 3, and every integer occurs once in the input array. So, any combination of 3 integers from the input array can be the answer.
CONSTRAINTS AND ASSUMPTIONS
1 <= input.length <= 10000.
Input integers are in the range of Integer.MIN_VALUE and Integer.MAX_VALUE.
Input integers do not have to be in any order.
1 <= N <= # of unique integers in input. For example, your input cannot be [1,2,2,3,3,3,10,11,12,100] while N = 12 because the number of unique integers (7) in the input is less than N (12).

Respuesta :

CODE:

import static java.util.stream.Collectors.*;

import java.lang.*;

import java.util.*;

import java.util.stream.*;

import java.util.stream.Collectors;

class Solution {

   //Sort the hash maps with respect to their values

   public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) {

       HashMap<Integer, Integer> temp= hm.entrySet().stream().sorted((i1, i2)

                                       -> i1.getValue().compareTo(i2.getValue()))

                                       .collect(Collectors.toMap(

                                               Map.Entry::getKey,

                                               Map.Entry::getValue,

                                               (e1, e2) -> e1, LinkedHashMap::new));

       return temp;

   }

   //return the least occurring integers in an array

   public int[] getN(int[] input, int k) {

       //create a array

       int result[] = new int[k];

       //create a hashmap which store the key-value pair

       HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();

       //iterate through the array

       for(int i=0; i<input.length; i++) {

           if( map.containsKey(input[i]) ) {

               map.put(input[i], map.get(input[i])+1);

               continue;

           }

           map.put(input[i],1);

       }

       //call the sortByValue() function to sort the values in the Hashmaps

       Map<Integer, Integer> hm1=sortByValue(map);

       int count=0;

       for (Map.Entry<Integer, Integer> en :hm1.entrySet()) {

           Integer intobject =en.getKey();

           // Returns the value of this Integer as an int

           int i = intobject.intValue();

           result[count]=i;

           count=count+1;

           if(count==k)

               break;

       }

       return result;

   }

}

public class Main {

   public static void main(String[] args) {

       //to take input from the user

       Scanner sc = new Scanner(System.in);

       Solution sol = new Solution();

       System.out.println("Enter the size of the Input array : ");

       int n =sc.nextInt();

       int arr[] = new int[n];

       int x;

       System.out.println("Enter the elements in Array : ");

       for(int i=0; i<n; i++) {

           x=sc.nextInt();

           arr[i]=x;

       }

       System.out.println("Enter how many least frequently occuring integer you want : ");

       int k=sc.nextInt();

       int[] output= sol.getN(arr,k);

       System.out.println("***************************************************************");

       System.out.println("The first "+k+" least occuring integer in array is : ");

       for(int i=0; i<output.length; i++)

           System.out.print(output[i]+"  ");

       System.out.println("\n***************************************************************");

   }

}

To read more about java integers, visit;

https://brainly.com/question/19672377

#SPJ4