Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 15, 2020 01:08
Show Gist options
  • Save parzibyte/8bc5b05465b882d05e4b0c577deb0d97 to your computer and use it in GitHub Desktop.
Save parzibyte/8bc5b05465b882d05e4b0c577deb0d97 to your computer and use it in GitHub Desktop.
// Mapa en donde almacenamos la frecuencia. Ver: https://parzibyte.me/blog/2020/01/07/hashmap-java-tutorial-ejemplos/
HashMap<String, Integer> mapaDeFrecuencias = new HashMap<>();
for (String palabra : palabras) {
if (mapaDeFrecuencias.containsKey(palabra)) {
mapaDeFrecuencias.put(palabra, mapaDeFrecuencias.get(palabra) + 1);
} else {
mapaDeFrecuencias.put(palabra, 1);
}
}
// Imprimir mapa
for (HashMap.Entry<String, Integer> entry : mapaDeFrecuencias.entrySet()) {
System.out.printf("Palabra '%s' con frecuencia %d\n", entry.getKey(), entry.getValue());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment