-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrintDuplicateCharacters.java
49 lines (43 loc) · 1.38 KB
/
PrintDuplicateCharacters.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.tryNew.one;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Created by sivan on 10/7/18.
*/
public class PrintDuplicateCharacters {
private Map<Character,Integer> mapDuplicate = new HashMap<>();
public void verifyDuplicate(String word)
{
if(word.isEmpty()){
}
char[] charWord = word.toCharArray();
for(char letter : charWord) {
if(mapDuplicate.containsKey(letter) && Character.isLetter(letter)){
mapDuplicate.put(letter,mapDuplicate.get(letter)+1);
}
else
mapDuplicate.put(letter,1);
}
printDuplicate();
}
public void printDuplicate(){
mapDuplicate.forEach((k,v)->{
if(v > 1 ) {
System.out.println("Character : "+ k + " Count : " + v);
}
});
/* for (Map.Entry<Character, Integer> entry : mapDuplicate.entrySet()) {
System.out.println("Item : " + entry.getKey() + " Count : " + entry.getValue());
}*/
}
public static void main(String[] args) {
PrintDuplicateCharacters obj = new PrintDuplicateCharacters();
Scanner sc = new Scanner(System.in);
String sword = sc.nextLine();
if(sword.isEmpty())
System.out.println("Input not given");
else
obj.verifyDuplicate(sword);
}
}