Created
February 20, 2015 17:57
-
-
Save rainiera/ba6f1f94d925f1a5d340 to your computer and use it in GitHub Desktop.
A method that returns an ArrayList of Strings that contain more than three of the same letter (case-insensitive), for the Names class of the NameSurfer project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns an ArrayList of Strings of names that contain three or more | |
* of the same letter. | |
* @return A list of the names that contain three or more of the same letter. | |
*/ | |
public ArrayList<String> tripleDuplicates() { | |
ArrayList<String> tripleDuplicates = new ArrayList<String>(); | |
for (NameRecord name : this.names) { | |
HashMap<Character, Integer> map = new HashMap<Character, Integer>(); | |
// I don't want case-sensitive letter frequencies | |
String tempName = name.name.toLowerCase(); | |
for (int i = 0; i < tempName.length(); i++) { | |
Character ch = tempName.charAt(i); | |
if (!map.containsKey(ch)) { | |
map.put(ch, 1); | |
} else { | |
int newVal = map.get(ch) + 1; | |
map.put(ch, newVal); | |
} | |
} | |
for (int i = 0; i < tempName.length(); i++) { | |
Character ch = tempName.charAt(i); | |
if (map.get(ch) >= 3 && !tripleDuplicates.contains(name.name)) { | |
tripleDuplicates.add(name.name); | |
} | |
} | |
} | |
return tripleDuplicates; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment