Skip to content

Instantly share code, notes, and snippets.

@rainiera
Created February 20, 2015 17:57
Show Gist options
  • Save rainiera/ba6f1f94d925f1a5d340 to your computer and use it in GitHub Desktop.
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.
/**
* 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