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