-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCollectionsHackerrankDemo.java
More file actions
151 lines (95 loc) · 3.79 KB
/
CollectionsHackerrankDemo.java
File metadata and controls
151 lines (95 loc) · 3.79 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package CollectionsHackerrank;
import java.util.*;
public class CollectionsHackerrankDemo {
public static void run() {
System.out.println("=== Collections HackerRank Practice ===");
problem1_removeDuplicates();
problem2_countWordFrequency();
problem3_reverseList();
problem4_sortWithTreeSet();
problem5_anagramCheck();
problem6_firstNonRepeatingCharacter();
problem7_queueSimulation();
problem8_stackExample();
problem9_studentGradesMap();
problem10_priorityQueueExample();
}
public static void problem1_removeDuplicates() {
List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5);
Set<Integer> unique = new LinkedHashSet<>(numbers);
System.out.println("1. Remove duplicates: " + unique);
}
public static void problem2_countWordFrequency() {
String sentence = "apple banana apple orange banana apple";
String[] words = sentence.split(" ");
Map<String, Integer> frequency = new HashMap<>();
for (String word : words) {
frequency.put(word, frequency.getOrDefault(word, 0) + 1);
}
System.out.println("2. Word frequency: " + frequency);
}
public static void problem3_reverseList() {
List<String> items = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));
Collections.reverse(items);
System.out.println("3. Reversed list: " + items);
}
public static void problem4_sortWithTreeSet() {
Set<Integer> numbers = new TreeSet<>(Arrays.asList(50, 10, 40, 20, 10, 30));
System.out.println("4. Sorted unique numbers: " + numbers);
}
public static void problem5_anagramCheck() {
String a = "listen";
String b = "silent";
char[] arr1 = a.toCharArray();
char[] arr2 = b.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
boolean isAnagram = Arrays.equals(arr1, arr2);
System.out.println("5. Are listen and silent anagrams? " + isAnagram);
}
public static void problem6_firstNonRepeatingCharacter() {
String word = "swiss";
Map<Character, Integer> frequency = new LinkedHashMap<>();
for (char c : word.toCharArray()) {
frequency.put(c, frequency.getOrDefault(c, 0) + 1);
}
Character firstUnique = null;
for (Map.Entry<Character, Integer> entry : frequency.entrySet()) {
if (entry.getValue() == 1) {
firstUnique = entry.getKey();
break;
}
}
System.out.println("6. First non-repeating character in 'swiss': " + firstUnique);
}
public static void problem7_queueSimulation() {
Queue<String> customers = new LinkedList<>();
customers.offer("Customer1");
customers.offer("Customer2");
customers.offer("Customer3");
System.out.println("7. Serving: " + customers.poll());
System.out.println(" Remaining queue: " + customers);
}
public static void problem8_stackExample() {
Stack<String> stack = new Stack<>();
stack.push("(");
stack.push("{");
stack.pop();
System.out.println("8. Stack contents: " + stack);
}
public static void problem9_studentGradesMap() {
Map<String, Integer> grades = new HashMap<>();
grades.put("Kenneth", 95);
grades.put("Jordan", 88);
grades.put("Chris", 91);
System.out.println("9. Student grades: " + grades);
}
public static void problem10_priorityQueueExample() {
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(25);
pq.offer(5);
pq.offer(15);
pq.offer(1);
System.out.println("10. Smallest item from PriorityQueue: " + pq.poll());
}
}