CC-16 : Find most common response option selected.
Description:
Given a String 2d matrix named resps with responses to a survey across multiple days (each row has responses for a particular day), find the most common response option selected across days. If the same response repeats on the same day, it will be counted as one response option only.
Test cases and expected outputs:
| Input Parameters | Expected outputs |
|---|---|
| resp={
{"yes","no","no","yes","yes"}, {"maybe","yes"}, {"maybe","no","no","yes","maybe","never"}} |
Most common response option : yes |
| resp={
{"yes","no","no","yes","yes"}, {"maybe"}, {"maybe","no","no","maybe","never"}} |
Most common response option : maybe,no |
Pseudocode:
| The java method should accept following input parameters: resps[][](String 2d matrix). |
| Initialize a variable idvRespMap of type HashMap |
| Initialize a variable allRespMap of type HashMap |
| Initialize an ArrayList< String> mostCommResp to hold most common selected response options. |
| Initialize an integer variable mostCommRespCnt to 0. |
| Iterate through resps using a for loop with allResp as a loop variable with initial value 0 and increment allResp till it reaches resps.length-1:
Clear entries in idvRespMap, as for each individual days responses, we want to start with an empty idvRespMap.
Iterate through resps using a for loop with idvResp as a loop variable with initial value 0 and increment idvResp till it reaches resps[allResp].length-1:
If idvRespMap does not contain key resps[allResp][ idvResp], then:
Put key-value pair resps[allResp][ idvResp]and 1 to idvRespMap.
If allRespMap contains key resps[allResp][ idvResp]with value freq, then put key-value pair resps[allResp][ idvResp] and freq+1 to allRespMap.
If allRespMap.get(resps[allResp][ idvResp]) > mostCommRespCnt:
Set mostCommRespCnt to allRespMap.get(resps[allResp][ idvResp]).
Remove all existing entries from mostCommResp as we have found a new higher mostCommRespCnt.
Add resps[allResp][ idvResp] to mostCommonResps.
Else:
If allRespMap.get(resps[allResp][ idvResp]) == mostCommRespCnt, then Add resps[allResp][ idvResp] to mostCommonResps, as we have found a duplicate most common response option.
|
| After above loop completes, mostCommonResps contains the most common response option selected across days. Return mostCommonResps.toArray(). |
Code:
public String[] hashMapFindMostCommonResponse(String[][] resps) throws Exception{
HashMap<String,Integer> idvRespMap=new HashMap<String, Integer>();
HashMap<String,Integer> allRespMap=new HashMap<String, Integer>();
ArrayList<String> mostCommResp=new ArrayList<String>();
int mostCommRespCnt=0;
for (int allResp=0; allResp < resps.length; allResp++) {
idvRespMap.clear();
for (int idvResp=0; idvResp < resps[allResp].length; idvResp++) {
if (!idvRespMap.containsKey(resps[allResp][idvResp])) {
idvRespMap.put(resps[allResp][idvResp],1);
allRespMap.put(resps[allResp][idvResp], allRespMap.getOrDefault(resps[allResp][idvResp], 0)+1);
if (allRespMap.get(resps[allResp][idvResp]) > mostCommRespCnt) {
mostCommRespCnt=allRespMap.get(resps[allResp][idvResp]);
mostCommResp.clear();
mostCommResp.add(resps[allResp][idvResp]);
}else {
if (allRespMap.get(resps[allResp][idvResp]) == mostCommRespCnt) {
mostCommResp.add(resps[allResp][idvResp]);
}
}
}
}
}
return mostCommResp.toArray(new String[0]);
}
Click here to download and run code and test cases !
| About Us | Privacy Policy | Contact us |