forked from Kaustubh72/Hackkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindduplicate.java
More file actions
55 lines (45 loc) · 1.44 KB
/
findduplicate.java
File metadata and controls
55 lines (45 loc) · 1.44 KB
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
50
51
52
53
54
55
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Details {
public void countDupChars(String str){
//Create a HashMap
Map<Character, Integer> map = new HashMap<Character, Integer>();
//Convert the String to char array
char[] chars = str.toCharArray();
/* logic: char are inserted as keys and their count
* as values. If map contains the char already then
* increase the value by 1
*/
for(Character ch:chars){
if(map.containsKey(ch)){
map.put(ch, map.get(ch)+1);
} else {
map.put(ch, 1);
}
}
//Obtaining set of keys
Set<Character> keys = map.keySet();
/* Display count of chars if it is
* greater than 1. All duplicate chars would be
* having value greater than 1.
*/
for(Character ch:keys){
if(map.get(ch) > 1){
System.out.println("Char "+ch+" "+map.get(ch));
}
}
}
public static void main(String a[]){
Details obj = new Details();
System.out.println("String: HellloHacktober");
System.out.println("-------------------------");
obj.countDupChars(" HellloHacktober");
System.out.println("\nString: AshifaFatima");
System.out.println("-------------------------");
obj.countDupChars("AshifaFatima");
System.out.println("\nString: #@$@!#$%!!%@");
System.out.println("-------------------------");
obj.countDupChars("#@$@!#$%!!%@");
}
}