From afe56a01fd176fbe326a33211785be51ed457be7 Mon Sep 17 00:00:00 2001 From: Bryant Ferguson Date: Wed, 4 Mar 2026 16:58:15 -0500 Subject: [PATCH 1/6] finished iterable --- .../java/Iterable/Examples/ForEachLoopDemo.java | 12 +++++++++--- src/main/java/Iterable/Examples/IteratorDemo.java | 10 ++++++++-- .../java/Iterable/Practice/IterableWarmups.java | 13 +++++++++++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/main/java/Iterable/Examples/ForEachLoopDemo.java b/src/main/java/Iterable/Examples/ForEachLoopDemo.java index a6d2198..d1ed006 100644 --- a/src/main/java/Iterable/Examples/ForEachLoopDemo.java +++ b/src/main/java/Iterable/Examples/ForEachLoopDemo.java @@ -17,13 +17,17 @@ public static void main(String[] args) { // TODO: // Use a for-each loop to print each student name - + for(String student : students){ + System.out.println(student); + } System.out.println("\nPrinting students in uppercase:"); // TODO: // Use a for-each loop to print each name in uppercase - + for(String student : students){ + System.out.println(student.toUpperCase()); + } System.out.println("\nCount the number of students:"); @@ -31,7 +35,9 @@ public static void main(String[] args) { // TODO: // Use a for-each loop to count how many students are in the list - + for(String student : students){ + count++; + } System.out.println("Total students: " + count); } } diff --git a/src/main/java/Iterable/Examples/IteratorDemo.java b/src/main/java/Iterable/Examples/IteratorDemo.java index b09fca7..6c08e8d 100644 --- a/src/main/java/Iterable/Examples/IteratorDemo.java +++ b/src/main/java/Iterable/Examples/IteratorDemo.java @@ -26,7 +26,9 @@ public static void main(String[] args) { // TODO: // Use iterator.hasNext() and iterator.next() // Print each number - + while(iterator.hasNext()){ + System.out.println(iterator.next()); + } System.out.println("\nRemoving odd numbers using Iterator"); @@ -35,7 +37,11 @@ public static void main(String[] args) { // TODO: // Use iterator to remove odd numbers // Remember: use iterator.remove() - + while(iterator.hasNext()){ + if(iterator.next() %2 != 0){ + iterator.remove(); + } + } System.out.println("\nUpdated list:"); System.out.println(numbers); diff --git a/src/main/java/Iterable/Practice/IterableWarmups.java b/src/main/java/Iterable/Practice/IterableWarmups.java index 9e9de94..618e814 100644 --- a/src/main/java/Iterable/Practice/IterableWarmups.java +++ b/src/main/java/Iterable/Practice/IterableWarmups.java @@ -30,7 +30,9 @@ public static int sum(Iterable numbers) { // TODO: // Use a for-each loop to calculate the sum - + for(int number:numbers){ + total += number; + } return total; } @@ -46,7 +48,9 @@ public static int countEven(Iterable numbers) { // TODO: // Loop through numbers // Increment count if number is even - + for (int number:numbers){ + count++; + } return count; } @@ -63,6 +67,11 @@ public static int findMax(Iterable numbers) { // Loop through numbers // Update max if current number is larger + for(int number:numbers){ + if(max < number){ + max = number; + } + } return max; } From 0de31379fd8fe7dc386c6b3ac02de8a8507373e3 Mon Sep 17 00:00:00 2001 From: Bryant Ferguson Date: Wed, 4 Mar 2026 19:23:53 -0500 Subject: [PATCH 2/6] finished collection basic and arraylist problems --- .../Practice/CollectionBasics.java | 41 +++++++++++++---- .../Lists/ArrayLists/ArrayListProblems.java | 46 +++++++++++++------ 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/main/java/Collections/Practice/CollectionBasics.java b/src/main/java/Collections/Practice/CollectionBasics.java index e45cb49..5f66a78 100644 --- a/src/main/java/Collections/Practice/CollectionBasics.java +++ b/src/main/java/Collections/Practice/CollectionBasics.java @@ -1,7 +1,10 @@ package Collections.Practice; +import javax.swing.*; import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; +import java.util.List; public class CollectionBasics { public static void main(String[] args) { @@ -19,6 +22,7 @@ public static void main(String[] args) { System.out.println("Count of even numbers: " + countEven(numbers)); System.out.println("Largest number: " + findMax(numbers)); System.out.println("Contains duplicates? " + hasDuplicates(numbers)); + System.out.println("Numbers greater than 20: " + filterGreaterThanTwenty(numbers)); } @@ -33,6 +37,9 @@ public static int sum(Collection numbers) { // TODO: // Loop through the collection // Add each number to total + for(Integer number: numbers){ + total += number; + } return total; } @@ -49,7 +56,11 @@ public static int countEven(Collection numbers) { // TODO: // Loop through the collection // If the number is even, increase count - + for(Integer number: numbers){ + if(number %2 == 0){ + count++; + } + } return count; } @@ -65,6 +76,11 @@ public static int findMax(Collection numbers) { // TODO: // Loop through numbers // Update max if current number is larger + for(Integer number: numbers){ + if(max < number){ + max = number; + } + } return max; } @@ -76,10 +92,13 @@ public static int findMax(Collection numbers) { Return false otherwise */ public static boolean hasDuplicates(Collection numbers) { - - // TODO: - // Hint: - // Compare the size of a collection with the size of a Set + List seen = new ArrayList<>(); + for (Integer num : numbers) { + if (seen.contains(num)) { + return true; + } + seen.add(num); + } return false; } @@ -96,7 +115,9 @@ public static int countOccurrences(Collection numbers, int target) { // TODO: // Loop through numbers // If number equals target, increase count - + for (Integer number: numbers){ + count += 1; + } return count; } @@ -112,8 +133,12 @@ public static Collection filterGreaterThanTwenty(Collection nu // TODO: // Loop through numbers - // Add numbers greater than 20 to result - + // Add numbers greater than 20 to result\ + for (Integer number:numbers){ + if (number > 20) { + result.add(number); + } + } return result; } } diff --git a/src/main/java/Lists/ArrayLists/ArrayListProblems.java b/src/main/java/Lists/ArrayLists/ArrayListProblems.java index baf4dfa..b596e8a 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -34,8 +34,11 @@ public static void main(String[] args) { public static int sum(List nums) { // TODO: Implement this method - - return 0; + int total = 0; + for (int num: nums){ + total += num; + } + return total; } /* @@ -47,10 +50,14 @@ public static int sum(List nums) { Output: 2 */ public static int countEvens(List nums) { - // TODO: Implement this method - - return 0; + int counter = 0; + for (int num: nums){ + if(num %2 == 0){ + counter++; + } + } + return counter; } /* @@ -65,10 +72,15 @@ public static int countEvens(List nums) { Output: false */ public static boolean hasDuplicate(List nums) { - // TODO: Implement this method - - return false; + List seen = new ArrayList<>(); //creates a new empty list + for (Integer num : nums) { //cycles through the list of numbers + if (seen.contains(num)) { //if both lists contain the same number + return true; //returns true + } + seen.add(num); //otherwise add it to the new list + } + return false; //return false } /* @@ -80,10 +92,14 @@ public static boolean hasDuplicate(List nums) { Output: 7 */ public static int findMax(List nums) { - // TODO: Implement this method - - return 0; + int max = nums.get(0); + for (Integer num : nums){ + if (max < num){ + max = num; + } + } + return max; } /* @@ -97,9 +113,11 @@ public static int findMax(List nums) { The original list should remain unchanged. */ public static List reverse(List nums) { - // TODO: Implement this method - - return null; + List reverseList = new ArrayList<>(); + for (int i = nums.size()-1; i >= 0; i--) { + reverseList.add(nums.get(i)); + } + return reverseList; } } From cc9717a136662e525842f076e4e845682d2a54ea Mon Sep 17 00:00:00 2001 From: Bryant Ferguson Date: Wed, 4 Mar 2026 19:50:56 -0500 Subject: [PATCH 3/6] finished LinkedLists --- .../java/Lists/LinkedLists/LinkedListProblems.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/Lists/LinkedLists/LinkedListProblems.java b/src/main/java/Lists/LinkedLists/LinkedListProblems.java index 178a2ba..ddae1d5 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -1,5 +1,6 @@ package Lists.LinkedLists; +import java.util.Collections; import java.util.LinkedList; public class LinkedListProblems { @@ -38,7 +39,7 @@ public static void main(String[] args) { public static void addToFront(LinkedList list, int value) { // TODO: Implement this method - + list.addFirst(value); } /* @@ -52,6 +53,7 @@ public static void addToFront(LinkedList list, int value) { public static void addToEnd(LinkedList list, int value) { // TODO: Implement this method + list.addLast(value); } @@ -66,6 +68,7 @@ public static void addToEnd(LinkedList list, int value) { public static void removeFirstElement(LinkedList list) { // TODO: Implement this method + list.removeFirst(); } @@ -80,7 +83,7 @@ public static void removeFirstElement(LinkedList list) { public static void removeLastElement(LinkedList list) { // TODO: Implement this method - + list.removeLast(); } /* @@ -95,7 +98,7 @@ public static int getFirstElement(LinkedList list) { // TODO: Implement this method - return 0; + return list.getFirst(); } /* @@ -110,6 +113,6 @@ public static int getLastElement(LinkedList list) { // TODO: Implement this method - return 0; + return list.getLast(); } } From 1f2c821a2e95241bcf6e7dfcc0a604d2169269dc Mon Sep 17 00:00:00 2001 From: Bryant Ferguson Date: Wed, 4 Mar 2026 21:26:26 -0500 Subject: [PATCH 4/6] finished queues --- .../Practice/CollectionBasics.java | 5 +- .../Queues/ArrayDeque/ArrayDequeProblems.java | 14 ++--- src/main/java/Queues/Deque/DequeProblems.java | 56 ++++++++++++++----- .../java/Sets/HashSet/HashSetProblems.java | 51 ++++++++++++++--- 4 files changed, 94 insertions(+), 32 deletions(-) diff --git a/src/main/java/Collections/Practice/CollectionBasics.java b/src/main/java/Collections/Practice/CollectionBasics.java index 5f66a78..9321065 100644 --- a/src/main/java/Collections/Practice/CollectionBasics.java +++ b/src/main/java/Collections/Practice/CollectionBasics.java @@ -103,7 +103,6 @@ public static boolean hasDuplicates(Collection numbers) { return false; } - /* PROBLEM 5 Count how many times a target value appears @@ -116,7 +115,9 @@ public static int countOccurrences(Collection numbers, int target) { // Loop through numbers // If number equals target, increase count for (Integer number: numbers){ - count += 1; + if(number == target){ + count++; + } } return count; } diff --git a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java index dac03cf..8bd6d8e 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -31,7 +31,7 @@ public static void main(String[] args) { public static void addToFront(ArrayDeque deque, int value) { // TODO: Implement this method - + deque.addFirst(value); } /* @@ -45,7 +45,7 @@ public static void addToFront(ArrayDeque deque, int value) { public static void addToBack(ArrayDeque deque, int value) { // TODO: Implement this method - + deque.addLast(value); } /* @@ -59,7 +59,7 @@ public static void addToBack(ArrayDeque deque, int value) { public static void removeFront(ArrayDeque deque) { // TODO: Implement this method - + deque.removeFirst(); } /* @@ -73,7 +73,7 @@ public static void removeFront(ArrayDeque deque) { public static void removeBack(ArrayDeque deque) { // TODO: Implement this method - + deque.removeLast(); } /* @@ -87,8 +87,7 @@ public static void removeBack(ArrayDeque deque) { public static Integer peekFront(ArrayDeque deque) { // TODO: Implement this method - - return null; + return deque.getFirst(); } /* @@ -102,7 +101,6 @@ public static Integer peekFront(ArrayDeque deque) { public static Integer peekBack(ArrayDeque deque) { // TODO: Implement this method - - return null; + return deque.getLast(); } } diff --git a/src/main/java/Queues/Deque/DequeProblems.java b/src/main/java/Queues/Deque/DequeProblems.java index 7ef0c06..58fb261 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -1,12 +1,46 @@ package Queues.Deque; +import java.util.ArrayDeque; import java.util.Deque; public class DequeProblems { public static void main(String[] args) { // You can test your methods here - + Deque deque = new ArrayDeque<>(); + + // ---------- Problem 1 ---------- + System.out.println("Problem 1: addFront"); + addFront(deque, 5); + System.out.println(deque); // Expected: [5] + + // ---------- Problem 2 ---------- + System.out.println("\nProblem 2: addBack"); + addBack(deque, 10); + addBack(deque, 15); + System.out.println(deque); // Expected: [5, 10, 15] + + // ---------- Problem 3 ---------- + System.out.println("\nProblem 3: removeFront"); + Integer removedFront = removeFront(deque); + System.out.println("Removed: " + removedFront); // Expected: 5 + System.out.println(deque); // Expected: [10, 15] + + // ---------- Problem 4 ---------- + System.out.println("\nProblem 4: removeBack"); + Integer removedBack = removeBack(deque); + System.out.println("Removed: " + removedBack); // Expected: 15 + System.out.println(deque); // Expected: [10] + + // ---------- Problem 5 ---------- + System.out.println("\nProblem 5: peekFront"); + System.out.println("Front: " + peekFront(deque)); // Expected: 10 + System.out.println(deque); // Should still be [10] + + // ---------- Problem 6 ---------- + System.out.println("\nProblem 6: peekBack"); + System.out.println("Back: " + peekBack(deque)); // Expected: 10 + System.out.println(deque); // Should still be [10] } /* @@ -20,7 +54,7 @@ public static void main(String[] args) { public static void addFront(Deque deque, int value) { // TODO: Implement this method - + deque.addFirst(value); } /* @@ -32,9 +66,8 @@ public static void addFront(Deque deque, int value) { Output: [5,10] */ public static void addBack(Deque deque, int value) { - // TODO: Implement this method - + deque.addLast(value); } /* @@ -46,10 +79,8 @@ public static void addBack(Deque deque, int value) { Output: 5 */ public static Integer removeFront(Deque deque) { - // TODO: Implement this method - - return null; + return deque.removeFirst(); } /* @@ -61,10 +92,8 @@ public static Integer removeFront(Deque deque) { Output: 15 */ public static Integer removeBack(Deque deque) { - // TODO: Implement this method - - return null; + return deque.removeLast(); } /* @@ -78,8 +107,7 @@ public static Integer removeBack(Deque deque) { public static Integer peekFront(Deque deque) { // TODO: Implement this method - - return null; + return deque.getFirst(); } /* @@ -91,10 +119,8 @@ public static Integer peekFront(Deque deque) { Output: 15 */ public static Integer peekBack(Deque deque) { - // TODO: Implement this method - - return null; + return deque.getLast(); } } diff --git a/src/main/java/Sets/HashSet/HashSetProblems.java b/src/main/java/Sets/HashSet/HashSetProblems.java index a36c570..1ad0530 100644 --- a/src/main/java/Sets/HashSet/HashSetProblems.java +++ b/src/main/java/Sets/HashSet/HashSetProblems.java @@ -1,5 +1,6 @@ package Sets.HashSet; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -7,7 +8,39 @@ public class HashSetProblems { public static void main(String[] args) { // You can test your methods here + // ---------- Problem 1 Test ---------- + System.out.println("Problem 1: addElement"); + Set fruits = new HashSet<>(); + addElement(fruits, "apple"); + System.out.println(fruits); // Expected: [apple] + + // ---------- Problem 2 Test ---------- + System.out.println("\nProblem 2: containsValue"); + addElement(fruits, "banana"); + System.out.println(containsValue(fruits, "banana")); // Expected: true + System.out.println(containsValue(fruits, "grape")); // Expected: false + + + // ---------- Problem 3 Test ---------- + System.out.println("\nProblem 3: removeValue"); + removeValue(fruits, "apple"); + System.out.println(fruits); // Expected: [banana] + + + // ---------- Problem 4 Test ---------- + System.out.println("\nProblem 4: getUniqueCount"); + addElement(fruits, "banana"); // duplicate attempt + addElement(fruits, "banana"); + addElement(fruits, "orange"); + System.out.println(getUniqueCount(fruits)); // Expected: 2 + + + // ---------- Problem 5 Test ---------- + System.out.println("\nProblem 5: getUniqueValues"); + List numbers = List.of(1, 2, 2, 3, 3, 3); + Set uniqueNumbers = getUniqueValues(numbers); + System.out.println(uniqueNumbers); // Expected: [1, 2, 3] } /* @@ -21,7 +54,7 @@ public static void main(String[] args) { public static void addElement(Set set, String value) { // TODO: Implement this method - + set.add(value); } /* @@ -35,7 +68,9 @@ public static void addElement(Set set, String value) { public static boolean containsValue(Set set, String value) { // TODO: Implement this method - + if (set.contains(value)){ + return true; + } return false; } @@ -50,7 +85,7 @@ public static boolean containsValue(Set set, String value) { public static void removeValue(Set set, String value) { // TODO: Implement this method - + set.remove(value); } /* @@ -65,7 +100,7 @@ public static int getUniqueCount(Set set) { // TODO: Implement this method - return 0; + return set.size(); } /* @@ -77,9 +112,11 @@ public static int getUniqueCount(Set set) { Output: {1,2,3} */ public static Set getUniqueValues(List numbers) { - // TODO: Implement this method - - return null; + Set set = new HashSet<>(); + for (Integer number: numbers){ + set.add(number); + } + return set; } } From b0e6d177459f8ebe1f86c5408224873256fec671 Mon Sep 17 00:00:00 2001 From: Bryant Ferguson Date: Wed, 4 Mar 2026 22:04:00 -0500 Subject: [PATCH 5/6] finished linked hashset --- .../java/Maps/HashMap/HashMapProblems.java | 23 ++++++++++------- .../LinkedHashMap/LinkedHashMapProblems.java | 25 +++++++++++++++---- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/main/java/Maps/HashMap/HashMapProblems.java b/src/main/java/Maps/HashMap/HashMapProblems.java index 04b5567..d7f9e7f 100644 --- a/src/main/java/Maps/HashMap/HashMapProblems.java +++ b/src/main/java/Maps/HashMap/HashMapProblems.java @@ -35,7 +35,7 @@ public static void main(String[] args) { public static void addItem(Map map, String item, int quantity) { // TODO: Implement this method - + map.put(item,quantity); } /* @@ -49,8 +49,7 @@ public static void addItem(Map map, String item, int quantity) public static int getQuantity(Map map, String item) { // TODO: Implement this method - - return 0; + return map.get(item); } /* @@ -62,9 +61,8 @@ public static int getQuantity(Map map, String item) { Output: {"Bananas"=12} */ public static void updateQuantity(Map map, String item, int newQuantity) { - // TODO: Implement this method - + map.put(item,newQuantity); } /* @@ -78,7 +76,7 @@ public static void updateQuantity(Map map, String item, int new public static void removeItem(Map map, String item) { // TODO: Implement this method - + map.remove(item); } /* @@ -90,9 +88,16 @@ public static void removeItem(Map map, String item) { Output: {1=1, 2=2, 3=3} */ public static Map countFrequency(List numbers) { - // TODO: Implement this method - - return null; + Map map = new HashMap<>(); + for (Integer num : numbers) { + if (!map.containsKey(num)) { + map.put(num, 1); // first time seeing number + } else { + int count = map.get(num); // get current count + map.put(num, count + 1); // increase count + } + } + return map; } } diff --git a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java index e8bbdc2..b0dda3f 100644 --- a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java +++ b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java @@ -33,7 +33,7 @@ public static void main(String[] args) { public static void addStudent(Map map, String name, int grade) { // TODO: Implement this method - + map.put(name,grade); } /* @@ -47,7 +47,7 @@ public static void addStudent(Map map, String name, int grade) public static void updateGrade(Map map, String name, int newGrade) { // TODO: Implement this method - + map.put(name,newGrade); } /* @@ -61,6 +61,7 @@ public static void updateGrade(Map map, String name, int newGra public static void removeStudent(Map map, String name) { // TODO: Implement this method + map.remove(name); } @@ -75,8 +76,11 @@ public static void removeStudent(Map map, String name) { public static String getFirstInserted(Map map) { // TODO: Implement this method + for (String key : map.keySet()) { //we did keyset because the student names are the keys not the values + return key; // returns first one only + } - return null; + return null; // if map is empty } /* @@ -91,7 +95,18 @@ public static String getFirstInserted(Map map) { public static Map wordFrequency(List words) { // TODO: Implement this method - - return null; + Map map = new LinkedHashMap<>(); + + for (String word:words){ + if (!map.containsKey(word)) { + map.put(word, 1); + } + else{ + int counter = map.get(word); + map.put(word,counter++); + } + } + + return map; } } From 68ad26c64658bea9979f68d99cb3a3c8795f994f Mon Sep 17 00:00:00 2001 From: Bryant Ferguson Date: Fri, 6 Mar 2026 21:01:53 -0500 Subject: [PATCH 6/6] finished the lab --- .../CollectionsHackerrankProblems.java | 226 +++++++++++++++--- .../java/Maps/TreeMap/TreeMapProblems.java | 18 +- 2 files changed, 201 insertions(+), 43 deletions(-) diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java index 6c2f4e1..9fb377b 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -1,16 +1,83 @@ package CollectionsHackerrank; -import java.util.List; -import java.util.Map; -import java.util.Queue; +import java.security.Key; +import java.util.*; public class CollectionsHackerrankProblems { - public class CollectionsHackerrankPractice { - public static void main(String[] args) { - // You can test your methods here - + // ---------- Problem 1: Remove duplicates ---------- + System.out.println("Problem 1: Remove duplicates"); + List nums1 = new ArrayList<>(Arrays.asList(1,2,2,3,4,4,5)); + System.out.println("Before: " + nums1); + System.out.println("After: " + removeDuplicates(new ArrayList<>(nums1))); + System.out.println(); + + // ---------- Problem 2: Count frequency ---------- + System.out.println("Problem 2: Count frequency"); + List nums2 = Arrays.asList(1,2,2,3,3,3); + System.out.println("Input: " + nums2); + System.out.println("Frequency: " + countFrequency(nums2)); + System.out.println(); + + // ---------- Problem 3: First unique ---------- + System.out.println("Problem 3: First unique"); + List nums3 = Arrays.asList(4,5,1,2,0,4); + System.out.println("Input: " + nums3); + System.out.println("First unique: " + firstUnique(nums3)); + System.out.println(); + + // ---------- Problem 4: Two sum ---------- + System.out.println("Problem 4: Two sum"); + List nums4 = Arrays.asList(2,7,11,15); + int target = 9; + System.out.println("Numbers: " + nums4 + ", Target: " + target); + System.out.println("Two sum exists? " + twoSum(nums4, target)); + System.out.println(); + + // ---------- Problem 5: Count unique words ---------- + System.out.println("Problem 5: Count unique words"); + List words = Arrays.asList("apple","banana","apple","orange"); + System.out.println("Input: " + words); + System.out.println("Unique count: " + countUniqueWords(words)); + System.out.println(); + + // ---------- Problem 6: Reverse queue ---------- + System.out.println("Problem 6: Reverse queue"); + Queue queue = new LinkedList<>(Arrays.asList(1,2,3,4)); + System.out.println("Before: " + queue); + System.out.println("After: " + reverseQueue(new LinkedList<>(queue))); + System.out.println(); + + // ---------- Problem 7: Balanced parentheses ---------- + System.out.println("Problem 7: Balanced parentheses"); + String expr1 = "(())"; + String expr2 = "(()"; + System.out.println("Expression: " + expr1 + ", Balanced? " + isBalanced(expr1)); + System.out.println("Expression: " + expr2 + ", Balanced? " + isBalanced(expr2)); + System.out.println(); + + // ---------- Problem 8: Most frequent ---------- + System.out.println("Problem 8: Most frequent"); + List nums8 = Arrays.asList(1,3,2,3,4,3); + System.out.println("Input: " + nums8); + System.out.println("Most frequent: " + mostFrequent(nums8)); + System.out.println(); + + // ---------- Problem 9: Group by word length ---------- + System.out.println("Problem 9: Group words by length"); + List words9 = Arrays.asList("cat","dog","elephant","ant"); + System.out.println("Input: " + words9); + System.out.println("Grouped: " + groupByLength(words9)); + System.out.println(); + + // ---------- Problem 10: Max sliding window sum ---------- + System.out.println("Problem 10: Max sliding window sum"); + List nums10 = Arrays.asList(2,1,5,1,3,2); + int k = 3; + System.out.println("Numbers: " + nums10 + ", Window size: " + k); + System.out.println("Max sum: " + maxSlidingWindowSum(nums10, k)); + System.out.println(); } /* @@ -22,10 +89,14 @@ public static void main(String[] args) { Output: [1,2,3,4,5] */ public static List removeDuplicates(List numbers) { - // TODO: Implement this method - - return null; + ArrayList seen = new ArrayList<>(); + for (Integer number:numbers){ + if (!seen.contains(number)){ + seen.add(number); + } + } + return seen; } /* @@ -39,8 +110,17 @@ public static List removeDuplicates(List numbers) { public static Map countFrequency(List numbers) { // TODO: Implement this method - - return null; + Map map = new HashMap<>(); + for (Integer number:numbers){ + if (!map.containsKey(number)){ + map.put(number,1); + } + else{ + int counter = map.get(number); + map.put(number, ++counter); + } + } + return map; } /* @@ -52,10 +132,25 @@ public static Map countFrequency(List numbers) { Output: 5 */ public static Integer firstUnique(List numbers) { - // TODO: Implement this method - - return null; + Map map = new HashMap<>(); + for (Integer number:numbers){ + if (!map.containsKey(number)){ + map.put(number, 1); + } + else{ + int counter = map.get(number); + map.put(number, counter++); + } + } + + int unique = 0; + for (Map.Entry entry : map.entrySet()){ + if (entry.getValue()==1){ + unique = entry.getKey(); + } + } + return unique; } /* @@ -70,8 +165,13 @@ public static Integer firstUnique(List numbers) { */ public static boolean twoSum(List numbers, int target) { - // TODO: Implement this method - + Set seen = new HashSet<>(); + for (int num : numbers) { + if (seen.contains(target-num)) { + return true; + } + seen.add(num); + } return false; } @@ -85,9 +185,13 @@ public static boolean twoSum(List numbers, int target) { */ public static int countUniqueWords(List words) { - // TODO: Implement this method + Set unique = new HashSet<>(); - return 0; + for (String word : words) { + unique.add(word); + } + + return unique.size(); } /* @@ -99,10 +203,14 @@ public static int countUniqueWords(List words) { Output: [4,3,2,1] */ public static Queue reverseQueue(Queue queue) { - - // TODO: Implement this method - - return null; + Stack stack = new Stack<>(); + while (!queue.isEmpty()) { + stack.push(queue.remove()); + } + while (!stack.isEmpty()) { + queue.add(stack.pop()); + } + return queue; } /* @@ -118,9 +226,19 @@ public static Queue reverseQueue(Queue queue) { */ public static boolean isBalanced(String expression) { - // TODO: Implement this method - - return false; + Stack stack = new Stack<>(); + for (char c : expression.toCharArray()) { + if (c == '(') { + stack.push(c); + } + if (c == ')') { + if (stack.isEmpty()) { + return false; + } + stack.pop(); + } + } + return stack.isEmpty(); } /* @@ -133,9 +251,27 @@ public static boolean isBalanced(String expression) { */ public static Integer mostFrequent(List numbers) { - // TODO: Implement this method + Map map = new HashMap<>(); + for (int num : numbers) { + if (!map.containsKey(num)) { + map.put(num, 1); + } else { + map.put(num, map.get(num) + 1); + } + } + + int maxCount = 0; + int result = 0; + + for (Map.Entry entry : map.entrySet()) { - return null; + if (entry.getValue() > maxCount) { + maxCount = entry.getValue(); + result = entry.getKey(); + } + } + + return result; } /* @@ -152,10 +288,15 @@ public static Integer mostFrequent(List numbers) { } */ public static Map> groupByLength(List words) { - - // TODO: Implement this method - - return null; + Map> map = new HashMap<>(); + for (String word : words) { + int length = word.length(); + if (!map.containsKey(length)) { + map.put(length, new ArrayList<>()); + } + map.get(length).add(word); + } + return map; } /* @@ -170,9 +311,24 @@ public static Map> groupByLength(List words) { */ public static int maxSlidingWindowSum(List numbers, int k) { - // TODO: Implement this method + int maxSum = 0; + int windowSum = 0; + + for (int i = 0; i < k; i++) { + windowSum += numbers.get(i); + } + + maxSum = windowSum; + + for (int i = k; i < numbers.size(); i++) { - return 0; + windowSum += numbers.get(i); + windowSum -= numbers.get(i - k); + + maxSum = Math.max(maxSum, windowSum); + } + + return maxSum; } } -} + diff --git a/src/main/java/Maps/TreeMap/TreeMapProblems.java b/src/main/java/Maps/TreeMap/TreeMapProblems.java index b101d2e..b89361d 100644 --- a/src/main/java/Maps/TreeMap/TreeMapProblems.java +++ b/src/main/java/Maps/TreeMap/TreeMapProblems.java @@ -18,6 +18,7 @@ public static void main(String[] args) { removePlayer(rankings, 2); System.out.println("After removal: " + rankings); + System.out.println("Next rank after 1: " + getNextRank(rankings, 1)); } /* @@ -31,7 +32,7 @@ public static void main(String[] args) { public static void addPlayer(TreeMap map, int rank, String name) { // TODO: Implement this method - + map.put(rank,name); } /* @@ -43,10 +44,9 @@ Return the player with the highest ranking (smallest key). Output: "Jordan" */ public static String getTopPlayer(TreeMap map) { - // TODO: Implement this method - - return null; + Integer first = map.firstKey(); + return map.get(first); } /* @@ -60,8 +60,8 @@ Return the player with the lowest ranking (largest key). public static String getLowestPlayer(TreeMap map) { // TODO: Implement this method - - return null; + Integer last = map.lastKey(); + return map.get(last); } /* @@ -75,7 +75,7 @@ public static String getLowestPlayer(TreeMap map) { public static void removePlayer(TreeMap map, int rank) { // TODO: Implement this method - + map.remove(rank); } /* @@ -89,7 +89,9 @@ public static void removePlayer(TreeMap map, int rank) { public static Integer getNextRank(TreeMap map, int rank) { // TODO: Implement this method - + if (map.higherKey(rank)!= null){ + return map.higherKey(rank); + } return null; } }