Question: **Java language only** Directions:the code below tells me the first second and third numbers which are most relevant in the array, Add to the code

**Java language only**

Directions:the code below tells me the first second and third numbers which are most relevant in the array, Add to the code below making it tell me how many times the first, second and third most relevant numbers came up in the code Example below.

/*

EXAMPLE:

EX: Array=[9],[3],[3],[4],[3],[4],[3],[4],[58],[8],[9]

EX: Array=[21],[21],[25],[25],[25],[2],[25],[2],[2],[]

EX: Array=[60],[60],[15],[5],[5],[5],[5],[60],[8],[8]

EX: Array=[90],[4],[90],[4],[25],[25],[4],[25],[4],[4]

EX: Array=[50],[50],[50],[60],[60],[60],[60],[12],[12]

EX: Array=[20],[20],[5],[3],[8],[8],[8],[8],[5],[5]

OutPut Example:

First relevant number:Output:3,25,5,4,60,8

"most relevant numbers came up"(3 came up 4 times)(25 came up 4 times)(5 came up 4 times)(4 came up 5 times)(60 came up 4 times)(8 came up 4 times)

Second relevant number:Output:4,2,60,25,50,5

"Second most relevant numbers came up"(4 came up 4 times)(2 came up 3 times)(60 came up 3 times)(25 came up 3 times)(50 came up 3 times)(5 came up 3 times)

Third relevant number:9,21,8,90,12,20

"Third most relevant numbers"(9 came up 2 times)(21 came up 2 times)(8 came up 2 times)(90 came up 2 times)(12 came up 2 times)(20 came up 2 times)

*/

package chegg;

import java.util.Comparator;

import java.util.HashMap;

import java.util.Map;

import java.util.TreeMap;

public class Chegg {

public static TreeMap getFrequencyMap(int num[]){

HashMap counts= new HashMap();

for(int i=0; i

if(counts.containsKey(num[i])){

int c = counts.get(num[i]) + 1;

counts.put(num[i], c);

}

else{

counts.put(num[i], 1);

}

}

ValueComparator bvc = new ValueComparator(counts);

TreeMap sortedMap = new TreeMap(bvc);

sortedMap.putAll(counts);

return sortedMap;

}

public static void main(String args[]){

int num1[] = {3, 1,14,1,11,1,2,9,12,1,1,1,40,21,1,2,13,2,17,2,11,+ 1,1,14,22,5,2,11,16,9,21,14,8,4,2,2,3,1,12,8,5,16,4,11,7,4,14,1,6,7,1,+ 24,10,7,17,14,5,28,14,1,29,10,2,3,3,17,16,28,1,4,10,1,28,4,8,6,14,16,10,16,3,+ 3,1,6,1,10,6,17,20,6,31,2,7,21,12,25,1,5,9,18,26,4,11,4,2};

int num2[] = {20,6,16,31,26,03,2,9,12,1,1,1,40,21,1,2,13,2,17,2,11,+ 1,1,14,22,5,2,11,16,9,21,14,8,4,2,2,3,1,12,8,5,16,4,11,7,4,14,1,6,7,1,+ 24,10,7,17,14,5,28,14,1,29,10,2,3,3,17,16,28,1,4,10,1,28,4,8,6,14,16,10,16,3,+ 3,1,6,1,10,6,17,20,6,31,2,7,21,12,25,1,5,9,18,26,4,11,4,2,};

int num3[] = {60,60,15,5,5,5,5,60,8,8};

int num4[] = {90,4,90,4,25,25,4,25,4,};

int num5[] = {50,50,50,60,60,60,60,12,12};

int num6[] = {20,20,5,3,8,8,8,8,5,5};

TreeMap sortedMap1 = getFrequencyMap(num1);

TreeMap sortedMap2 = getFrequencyMap(num2);

TreeMap sortedMap3 = getFrequencyMap(num3);

TreeMap sortedMap4 = getFrequencyMap(num4);

TreeMap sortedMap5 = getFrequencyMap(num5);

TreeMap sortedMap6 = getFrequencyMap(num6);

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

if(i==0){

System.out.print("First Relevance Number: output:");

System.out.print(sortedMap1.keySet().toArray()[i] + ",");

System.out.print(sortedMap2.keySet().toArray()[i] + ",");

System.out.print(sortedMap3.keySet().toArray()[i] +",");

System.out.print(sortedMap4.keySet().toArray()[i] + ",");

System.out.print(sortedMap5.keySet().toArray()[i] +",");

System.out.print(sortedMap6.keySet().toArray()[i] + ",");

System.out.println();

}

if(i==1){

System.out.print("Second Relevance Number: output:");

System.out.print(sortedMap1.keySet().toArray()[i] + ",");

System.out.print(sortedMap2.keySet().toArray()[i] + ",");

System.out.print(sortedMap3.keySet().toArray()[i] +",");

System.out.print(sortedMap4.keySet().toArray()[i] + ",");

System.out.print(sortedMap5.keySet().toArray()[i] +",");

System.out.print(sortedMap6.keySet().toArray()[i] + ",");

System.out.println();

}

if(i==2){

System.out.print("Third Relevance Number: output:");

System.out.print(sortedMap1.keySet().toArray()[i] + ",");

System.out.print(sortedMap2.keySet().toArray()[i] + ",");

System.out.print(sortedMap3.keySet().toArray()[i] +",");

System.out.print(sortedMap4.keySet().toArray()[i] + ",");

System.out.print(sortedMap5.keySet().toArray()[i] +",");

System.out.print(sortedMap6.keySet().toArray()[i] + ",");

System.out.println();

}

}

}

}

class ValueComparator> implements Comparator{

Map base;

public ValueComparator(Map base){

this.base = base;

}

@Override

public int compare(T1 k1, T1 k2) {

T2 val1 = base.get(k1);

T2 val2 = base.get(k2);

return val2.compareTo(val1);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!