Created
June 10, 2018 15:11
-
-
Save yaminmhd/408a3be79e0454e5dee2ff25ba5691e4 to your computer and use it in GitHub Desktop.
MapRunner.java - Number of occurrences in a given string
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
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