/ Published in: Java
Calculate The Second Highest Frequency
Write a method that accepts a String and returns the character with the second highest frequency. For example "aabbbc" would return 'a'. If the string contains spaces, they can be disregarded from the count.
Write a method that accepts a String and returns the character with the second highest frequency. For example "aabbbc" would return 'a'. If the string contains spaces, they can be disregarded from the count.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; public class FrequencyFinder { findSecondHighestFreq(str); } char secondChar = 0; scanChar(inStr.toCharArray(), charMap); return b.getValue().compareTo(a.getValue()); } }); System.out.println("2nd most freq: key="+entryList.get(1).getKey()+", count="+entryList.get(1).getValue()); return secondChar; } for (char chLocal : inCharArr) { if (chLocal==' ') { // ignore space character continue; } if (!charMap.containsKey(chLocal)){ charMap.put(chLocal, 1); } else { charMap.put(chLocal, (charMap.get(chLocal)+1)); } } } }
URL: http://java.dzone.com/articles/thursday-code-puzzler-second