これをうまく試すことができます:
package com.stack.overflow.works.main;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @author sarath_sivan
*/
public class DuplicatesFinder {
public static void findDuplicates(String inputString) {
Map<Character, Integer> duplicatesMap = new HashMap<Character, Integer>();
char[] charArray = inputString.toCharArray();
for (Character ch : charArray) {
if (duplicatesMap.containsKey(ch)) {
duplicatesMap.put(ch, duplicatesMap.get(ch) + 1);
} else {
duplicatesMap.put(ch, 1);
}
}
Set<Character> keySet = duplicatesMap.keySet();
for (Character ch: keySet) {
if (duplicatesMap.get(ch) > 1) {
System.out.println("[INFO: CHARACTER " + ch + " IS DUPLICATE, OCCURENCE: " + duplicatesMap.get(ch) + " TIMES]");
}
}
}
public static void main(String[] args) {
DuplicatesFinder.findDuplicates("sarath kumar sivan");
}
}
次のような入力文字列「sarath kumar sivan」の簡単なテスト結果が生成されます。
[INFO: CHARACTER IS DUPLICATE, OCCURENCE: 2 TIMES]
[INFO: CHARACTER s IS DUPLICATE, OCCURENCE: 2 TIMES]
[INFO: CHARACTER r IS DUPLICATE, OCCURENCE: 2 TIMES]
[INFO: CHARACTER a IS DUPLICATE, OCCURENCE: 4 TIMES]