Skip to content

Instantly share code, notes, and snippets.

@akashkumarcs19
Created February 1, 2021 15:25
Show Gist options
  • Save akashkumarcs19/c29f9cd74ac4783f3964e044b13d615b to your computer and use it in GitHub Desktop.
Save akashkumarcs19/c29f9cd74ac4783f3964e044b13d615b to your computer and use it in GitHub Desktop.
import java.util.HashMap;
import java.util.Map;
class Duplicates {
public static Map<Character, Integer> countDuplicateCharacters(String string) {
var resultMap = new HashMap<Character, Integer>();
var tempMap = new HashMap<Character,Integer>();
// write your code here ...
string= string.toUpperCase();
char [] temp=string.toCharArray();
for (int i = 0; i <temp.length ; i++) {
int count = 0;
if (tempMap.containsKey(temp[i])) {
count = tempMap.get(temp[i]);
}
tempMap.put(temp[i], ++count);
}
for (char i:tempMap.keySet()) {
int count = 0;
if(tempMap.containsKey(i)) {
count = tempMap.get(i);
if (count > 1)
resultMap.put(i,count);
}
}
return resultMap;
}
public static void main(String[] args) {
String message = "Competitive Programming Can Be A Little Bit Tricky!";
System.out.println(countDuplicateCharacters(message));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment