Skip to content

Instantly share code, notes, and snippets.

@yaminmhd
Created June 10, 2018 15:11
Show Gist options
  • Save yaminmhd/408a3be79e0454e5dee2ff25ba5691e4 to your computer and use it in GitHub Desktop.
Save yaminmhd/408a3be79e0454e5dee2ff25ba5691e4 to your computer and use it in GitHub Desktop.
MapRunner.java - Number of occurrences in a given string
package collections;
import java.util.LinkedHashMap;
public class StringRunner {
public static void main(String[] args) {
String str = "This is a great thing";
String[] arr = str.toLowerCase().replaceAll("\\s", "").split("");
LinkedHashMap<String, Integer> arrayMap = new LinkedHashMap<>();
for (String a : arr) {
int value = 1;
if (!arrayMap.containsKey(a)) {
arrayMap.put(a, value);
} else {
int index = arrayMap.get(a);
index++;
arrayMap.put(a, index);
}
}
System.out.println(arrayMap);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment