Skip to content

Instantly share code, notes, and snippets.

@tremaineeto
Last active April 28, 2021 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tremaineeto/aa70591e2123a12929e673b7fe5ca8e5 to your computer and use it in GitHub Desktop.
Save tremaineeto/aa70591e2123a12929e673b7fe5ca8e5 to your computer and use it in GitHub Desktop.
public void keepCountOfCharactersInString(String str) {
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
if (!map.containsKey(str.charAt(i)) {
map.put(str.charAt(i), 1);
} else {
int currentCount = map.get(str.charAt(i));
currentCount++;
map.put(str.charAt(i), currentCount);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment