Given an array containing integers, find the integers with highest and lowest frequency and return the same.
| Input Parameters |
Expected outputs |
| arr[]={4,3,3,1,0,1};
|
Most Frequent Integer in array is : 3 Count: 2
Least Frequent Integer in array is : 4 Count: 1
|
| arr[]={4,4,4,4};
|
Most Frequent Integer in array is : 4 Count: 4
Least Frequent Integer in array is : 4 Count: 4
|
| arr[]={9,9,9,1,2,2,2};
|
Most Frequent Integer in array is : 9 Count: 3
Least Frequent Integer in array is : 1 Count: 1
|
| The java method should accept following input parameters: arr (int array).
|
| Initialize a variable hMap of type HashMap to hold the input integers and their frequencies.
|
| Initialize variables mostFreq and mostFreqCnt to hold the most frequent int in arr and its frequency.
|
| Initialize variables leastFreq and leastFreqCnt to hold the least frequent int in arr and its frequency.
|
| Using a for loop, iterate through arr, using idx as a loop variable with initial value 0 and increment idx till it reaches arr.length-1:
if hMap does not already have a key arr[idx], set currFreq to 0.
Else get value corresponding to arr[idx] from hMap and increment it by 1 before setting it to currFreq.
If currFreq > mostFreqCount:
Set mostFreq=arr[idx].
Set mostFreqCnt=currFreq.
If leastFerq == arr[idx]:
Or If leastFreq < leastFreqCount:
Set leastFreq=arr[idx].
Set leastFreqCnt=currFreq.
|
| After completion of above loop, mostFreq and mostFreqCnt contain the most frequent int in arr and its frequency. Similarly, leastFreq and leastFreqCnt contain the least frequent int in arr and its frequency. Set the value of above 4 variables in int array retVal. Return retVal.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
import java.util.HashMap;
public class HashMapFindElementWithHighestAndLowestFrequency {
public int[] arrayFindHighestAndLowestFreq(int[] arr) throws Exception{
HashMap<Integer,Integer> hMap=new HashMap<Integer, Integer>();
int mostFreq=0; int mostFreqCnt=Integer.MIN_VALUE;
int leastFreq=0; int leastFreqCnt=Integer.MAX_VALUE;
int currFreq=0;
for (int idx=0; idx < arr.length; idx++) {
currFreq=hMap.getOrDefault(arr[idx],0)+1;
hMap.put(arr[idx],currFreq);
if (currFreq > mostFreqCnt) {
mostFreq=arr[idx];
mostFreqCnt=currFreq;
}
if ((leastFreq==arr[idx])||(currFreq < leastFreqCnt)) {
leastFreq=arr[idx];
leastFreqCnt=currFreq;
}
}
int[] retArr=new int[4];
retArr[0]=mostFreq; retArr[1]=mostFreqCnt;
retArr[2]=leastFreq; retArr[3]=leastFreqCnt;
return retArr;
}
public static void main(String[] args) {
HashMapFindElementWithHighestAndLowestFrequency ap=new HashMapFindElementWithHighestAndLowestFrequency();
int[] retArr;
try {
int[] intArray1 = {4,3,3,1,0,1};
printArraySummary(intArray1, "Original Array:");
retArr=ap.arrayFindHighestAndLowestFreq(intArray1);
System.out.print("Most Frequent Integer in array is : "+retArr[0]+" Count: "+retArr[1]+ "\n");
System.out.print("Least Frequent Integer in array is : "+retArr[2]+" Count: "+retArr[3]+ "\n\n");
int[] intArray2 = {4,4,4,4};
printArraySummary(intArray2, "Original Array:");
retArr=ap.arrayFindHighestAndLowestFreq(intArray2);
System.out.print("Most Frequent Integer in array is : "+retArr[0]+" Count: "+retArr[1]+ "\n");
System.out.print("Least Frequent Integer in array is : "+retArr[2]+" Count: "+retArr[3]+ "\n\n");
int[] intArray3 = {9,9,9,1,2,2,2};
printArraySummary(intArray3, "Original Array:");
retArr=ap.arrayFindHighestAndLowestFreq(intArray3);
System.out.print("Most Frequent Integer in array is : "+retArr[0]+" Count: "+retArr[1]+ "\n");
System.out.print("Least Frequent Integer in array is : "+retArr[2]+" Count: "+retArr[3]+ "\n\n");
int[] intArray4 = {1,2};
printArraySummary(intArray4, "Original Array:");
retArr=ap.arrayFindHighestAndLowestFreq(intArray4);
System.out.print("Most Frequent Integer in array is : "+retArr[0]+" Count: "+retArr[1]+ "\n");
System.out.print("Least Frequent Integer in array is : "+retArr[2]+" Count: "+retArr[3]+ "\n\n");
int[] intArray5 = {2,3,4,5};
printArraySummary(intArray5, "Original Array:");
retArr=ap.arrayFindHighestAndLowestFreq(intArray5);
System.out.print("Most Frequent Integer in array is : "+retArr[0]+" Count: "+retArr[1]+ "\n");
System.out.print("Least Frequent Integer in array is : "+retArr[2]+" Count: "+retArr[3]+ "\n\n");
int[] intArray6 = {5,5,5,6,6,6};
printArraySummary(intArray6, "Original Array:");
retArr=ap.arrayFindHighestAndLowestFreq(intArray6);
System.out.print("Most Frequent Integer in array is : "+retArr[0]+" Count: "+retArr[1]+ "\n");
System.out.print("Least Frequent Integer in array is : "+retArr[2]+" Count: "+retArr[3]+ "\n\n");
int[] intArray7 = {0};
printArraySummary(intArray7, "Original Array:");
retArr=ap.arrayFindHighestAndLowestFreq(intArray7);
System.out.print("Most Frequent Integer in array is : "+retArr[0]+" Count: "+retArr[1]+ "\n");
System.out.print("Least Frequent Integer in array is : "+retArr[2]+" Count: "+retArr[3]+ "\n\n");
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}