-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicate_String_Map_DS.java
More file actions
53 lines (41 loc) · 1.63 KB
/
Copy pathDuplicate_String_Map_DS.java
File metadata and controls
53 lines (41 loc) · 1.63 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
package com.rushi.practice;
import com.sun.jdi.IntegerValue;
import java.util.*;
public class Main {
// i am very lazy to type System.out.println(), that's why i created this function.
/** Accepts any Datatype value to print*/
public static void print(Object... s){
for (Object val : s){
System.out.print(val.toString());
}
}
///main function.
public static void main(String[] argv) {
Scanner input = new Scanner(System.in);
//creating the instace of Map.
Map m = new TreeMap();
int value = 0;
int count = 1;
String[] arr = {"a", "b", "c", "a", "d", "e", "b", "c", "a", "e", "d"};
//iterating through every element in the array.
for (String val: arr){
// checking if the given element is present in the array.
if (m.containsKey(val)){
//getting the KeyValue( no of time repeated ) for string.
value = Integer.parseInt(m.get(val).toString());
value++;
//replacing it new value.
m.replace(val, value);
// printing every iterations after finding duplicate strings.
print("Iteration ", count++, " : ");
print(m, "\n");
}
// if the element is not present in the Map then add it.
else
m.put(val, 0);
}
print("\nFinal Iteration: ");
print(m);
input.close();
}
}