diff --git a/src/main/java/Collections/Practice/CollectionBasics.java b/src/main/java/Collections/Practice/CollectionBasics.java index e45cb49..b7c0e24 100644 --- a/src/main/java/Collections/Practice/CollectionBasics.java +++ b/src/main/java/Collections/Practice/CollectionBasics.java @@ -33,6 +33,9 @@ public static int sum(Collection numbers) { // TODO: // Loop through the collection // Add each number to total + for (Integer num : numbers) { + total += num; + } return total; } @@ -48,7 +51,12 @@ public static int countEven(Collection numbers) { // TODO: // Loop through the collection + for (Integer num : numbers) { // If the number is even, increase count + if (num % 2 == 0) { + count++; + } + } return count; } @@ -64,7 +72,13 @@ public static int findMax(Collection numbers) { // TODO: // Loop through numbers + for (Integer num : numbers) { // Update max if current number is larger + if (num > max) { + max = num; + } + } + return max; } @@ -77,10 +91,15 @@ public static int findMax(Collection numbers) { */ public static boolean hasDuplicates(Collection numbers) { + + + // TODO: // Hint: // Compare the size of a collection with the size of a Set + + return false; } @@ -96,6 +115,11 @@ public static int countOccurrences(Collection numbers, int target) { // TODO: // Loop through numbers // If number equals target, increase count + for (Integer num : numbers) { + if (num == target) { + count++; + } + } return count; } @@ -112,8 +136,15 @@ public static Collection filterGreaterThanTwenty(Collection nu // TODO: // Loop through numbers - // Add numbers greater than 20 to result + for (Integer num : numbers) { + // Add numbers greater than 20 to result + if (num > 20) { + result.add(num); + + } + } + return result; } -} + } diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java index 6c2f4e1..7a62729 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -1,15 +1,28 @@ package CollectionsHackerrank; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Queue; +import java.util.Set; +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Stack; + public class CollectionsHackerrankProblems { - public class CollectionsHackerrankPractice { + public static class CollectionsHackerrankPractice { public static void main(String[] args) { // You can test your methods here + List numbers = Arrays.asList(1,2,2,3,4,4,5); + System.out.println(removeDuplicates(numbers)); + } + + } @@ -24,8 +37,14 @@ public static void main(String[] args) { public static List removeDuplicates(List numbers) { // TODO: Implement this method + Set uniqueNumbers = new LinkedHashSet<>(numbers); // removes duplicates + List result = new ArrayList<>(uniqueNumbers); + + + + + return result; - return null; } /* @@ -39,10 +58,22 @@ public static List removeDuplicates(List numbers) { public static Map countFrequency(List numbers) { // TODO: Implement this method + Map frequencyMap = new HashMap<>(); - return null; + for (Integer num : numbers) { + if (frequencyMap.containsKey(num)) { + frequencyMap.put(num, frequencyMap.get(num) + 1); + } else { + frequencyMap.put(num, 1); + } + } + + return frequencyMap; } + + + /* Problem 3 Return the first number that appears only once. @@ -54,6 +85,18 @@ public static Map countFrequency(List numbers) { public static Integer firstUnique(List numbers) { // TODO: Implement this method + Map frequencyMap = new HashMap<>(); + + for (Integer num : numbers) { + frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); + } + + for (Integer num : numbers) { + if (frequencyMap.get(num) == 1) { + return num; + } + } + return null; } @@ -71,10 +114,23 @@ 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) { + int complement = target - num; + + if (seen.contains(complement)) { + return true; + } + + seen.add(num); + } return false; } + + /* Problem 5 Count how many unique words exist in a list. @@ -86,10 +142,16 @@ public static boolean twoSum(List numbers, int target) { public static int countUniqueWords(List words) { // TODO: Implement this method + Set uniqueWords = new HashSet<>(); + + for (String word : words) { + uniqueWords.add(word); + } - return 0; + return uniqueWords.size(); } + /* Problem 6 Reverse a queue. @@ -101,10 +163,23 @@ public static int countUniqueWords(List words) { public static Queue reverseQueue(Queue queue) { // TODO: Implement this method + Stack stack = new Stack<>(); - return null; + // Move elements from queue to stack + while (!queue.isEmpty()) { + stack.push(queue.poll()); + } + + // Move elements back to queue + while (!stack.isEmpty()) { + queue.add(stack.pop()); + } + + return queue; } + + /* Problem 7 Determine whether parentheses are balanced. @@ -119,10 +194,28 @@ public static Queue reverseQueue(Queue queue) { public static boolean isBalanced(String expression) { // TODO: Implement this method + Stack stack = new Stack<>(); - return false; + for (char c : expression.toCharArray()) { + + if (c == '(') { + stack.push(c); + } + else if (c == ')') { + + if (stack.isEmpty()) { + return false; + } + + stack.pop(); + } + } + + return stack.isEmpty(); } + + /* Problem 8 Return the number that appears most frequently in the list. @@ -134,10 +227,27 @@ public static boolean isBalanced(String expression) { public static Integer mostFrequent(List numbers) { // TODO: Implement this method + Map frequency = new HashMap<>(); - return null; + for (Integer num : numbers) { + frequency.put(num, frequency.getOrDefault(num, 0) + 1); + } + + int maxCount = 0; + Integer result = null; + + for (Map.Entry entry : frequency.entrySet()) { + if (entry.getValue() > maxCount) { + maxCount = entry.getValue(); + result = entry.getKey(); + } + } + + return result; } + + /* Problem 9 Group words based on their length. @@ -154,10 +264,24 @@ public static Integer mostFrequent(List numbers) { public static Map> groupByLength(List words) { // TODO: Implement this method + Map> map = new HashMap<>(); - return null; + for (String word : words) { + + int length = word.length(); + + if (!map.containsKey(length)) { + map.put(length, new ArrayList<>()); + } + + map.get(length).add(word); + } + + return map; } + + /* Problem 10 Return the maximum sum of any window of size k. @@ -171,8 +295,26 @@ public static Map> groupByLength(List words) { public static int maxSlidingWindowSum(List numbers, int k) { // TODO: Implement this method + int windowSum = 0; - return 0; + // First window + for (int i = 0; i < k; i++) { + windowSum += numbers.get(i); + } + + int maxSum = windowSum; + + // Slide the window + for (int i = k; i < numbers.size(); i++) { + windowSum += numbers.get(i); // add next + windowSum -= numbers.get(i - k); // remove previous + maxSum = Math.max(maxSum, windowSum); + } + + return maxSum; } + + + } -} + diff --git a/src/main/java/Iterable/Examples/ForEachLoopDemo.java b/src/main/java/Iterable/Examples/ForEachLoopDemo.java index a6d2198..4c79ebd 100644 --- a/src/main/java/Iterable/Examples/ForEachLoopDemo.java +++ b/src/main/java/Iterable/Examples/ForEachLoopDemo.java @@ -17,12 +17,20 @@ 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,6 +39,11 @@ 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) { + System.out.println(student.toUpperCase()); + } + + 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..6003ccd 100644 --- a/src/main/java/Iterable/Examples/IteratorDemo.java +++ b/src/main/java/Iterable/Examples/IteratorDemo.java @@ -26,6 +26,11 @@ public static void main(String[] args) { // TODO: // Use iterator.hasNext() and iterator.next() // Print each number + while (iterator.hasNext()) { + Integer number = iterator.next(); + System.out.println(number); + } + System.out.println("\nRemoving odd numbers using Iterator"); @@ -35,6 +40,13 @@ public static void main(String[] args) { // TODO: // Use iterator to remove odd numbers // Remember: use iterator.remove() + while (iterator.hasNext()) { + Integer number = iterator.next(); + + if (number % 2 != 0) { + iterator.remove(); + } + } System.out.println("\nUpdated list:"); diff --git a/src/main/java/Iterable/Practice/IterableWarmups.java b/src/main/java/Iterable/Practice/IterableWarmups.java index 9e9de94..84cc06a 100644 --- a/src/main/java/Iterable/Practice/IterableWarmups.java +++ b/src/main/java/Iterable/Practice/IterableWarmups.java @@ -30,6 +30,9 @@ public static int sum(Iterable numbers) { // TODO: // Use a for-each loop to calculate the sum + for (Integer num : numbers) { + total += num; + } return total; } @@ -46,11 +49,18 @@ public static int countEven(Iterable numbers) { // TODO: // Loop through numbers // Increment count if number is even + for (Integer num : numbers) { + if (num % 2 == 0) { + count++; + } + } return count; } + + /* PROBLEM 3 Return the maximum value @@ -62,11 +72,18 @@ public static int findMax(Iterable numbers) { // TODO: // Loop through numbers // Update max if current number is larger + for (Integer num : numbers) { + if (num > max) { + max = num; + } + } return max; } + + /* PROBLEM 4 (BONUS) Count how many times a word appears @@ -78,7 +95,14 @@ public static int countMatches(Iterable words, String target) { // TODO: // Loop through words // Compare each word to target + for (String word : words) { + if (word.equals(target)) { + count++; + } + } return count; } + + } diff --git a/src/main/java/Lists/ArrayLists/ArrayListProblems.java b/src/main/java/Lists/ArrayLists/ArrayListProblems.java index baf4dfa..97d5f2d 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -34,10 +34,21 @@ public static void main(String[] args) { public static int sum(List nums) { // TODO: Implement this method + int total = 0; - return 0; + for (int num : nums) { + total += num; + } + + return total; } + + + + + + /* Problem 2 Count how many EVEN numbers exist in the list. @@ -49,10 +60,19 @@ public static int sum(List nums) { public static int countEvens(List nums) { // TODO: Implement this method + int count = 0; + + for (int num : nums) { + if (num % 2 == 0) { + count++; + } + } - return 0; + return count; } + + /* Problem 3 Determine if the list contains any duplicate values. @@ -67,10 +87,19 @@ public static int countEvens(List nums) { public static boolean hasDuplicate(List nums) { // TODO: Implement this method + for (int i = 0; i < nums.size(); i++) { + for (int j = i + 1; j < nums.size(); j++) { + if (nums.get(i).equals(nums.get(j))) { + return true; + } + } + } return false; } + + /* Problem 4 Return the largest number in the list. @@ -82,10 +111,19 @@ public static boolean hasDuplicate(List nums) { public static int findMax(List nums) { // TODO: Implement this method + int max = nums.get(0); + + for (int num : nums) { + if (num > max) { + max = num; + } + } - return 0; + return max; } + + /* Problem 5 Return a NEW list that contains the elements of the original list in reverse order. @@ -100,6 +138,14 @@ public static List reverse(List nums) { // TODO: Implement this method - return null; + List reversed = new ArrayList<>(); + + for (int i = nums.size() - 1; i >= 0; i--) { + reversed.add(nums.get(i)); + } + + return reversed; } + + } diff --git a/src/main/java/Lists/LinkedLists/LinkedListProblems.java b/src/main/java/Lists/LinkedLists/LinkedListProblems.java index 178a2ba..885edaf 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -38,6 +38,8 @@ public static void main(String[] args) { public static void addToFront(LinkedList list, int value) { // TODO: Implement this method + list.addFirst(value); + } @@ -52,6 +54,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,7 +69,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(); } /* @@ -94,6 +97,9 @@ public static void removeLastElement(LinkedList list) { public static int getFirstElement(LinkedList list) { // TODO: Implement this method + if (!list.isEmpty()) { + list.removeLast(); + } return 0; } @@ -109,7 +115,9 @@ public static int getFirstElement(LinkedList list) { public static int getLastElement(LinkedList list) { // TODO: Implement this method - - return 0; + return list.getLast(); } + + + } diff --git a/src/main/java/Maps/HashMap/HashMapProblems.java b/src/main/java/Maps/HashMap/HashMapProblems.java index 04b5567..9f7f26d 100644 --- a/src/main/java/Maps/HashMap/HashMapProblems.java +++ b/src/main/java/Maps/HashMap/HashMapProblems.java @@ -35,9 +35,11 @@ public static void main(String[] args) { public static void addItem(Map map, String item, int quantity) { // TODO: Implement this method - + map.put(item, quantity); } + + /* Problem 2 Return the quantity of a specific item. @@ -49,10 +51,11 @@ 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); } + + /* Problem 3 Update the quantity of an existing item. @@ -64,9 +67,11 @@ public static int getQuantity(Map map, String item) { public static void updateQuantity(Map map, String item, int newQuantity) { // TODO: Implement this method - + map.put(item, newQuantity); } + + /* Problem 4 Remove an item from the map. @@ -78,9 +83,11 @@ 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); } + + /* Problem 5 Count how many times each number appears in a list. @@ -92,7 +99,18 @@ public static void removeItem(Map map, String item) { public static Map countFrequency(List numbers) { // TODO: Implement this method + Map frequencyMap = new HashMap<>(); - return null; + for (Integer num : numbers) { + if (frequencyMap.containsKey(num)) { + frequencyMap.put(num, frequencyMap.get(num) + 1); + } else { + frequencyMap.put(num, 1); + } + } + + return frequencyMap; } + + } diff --git a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java index e8bbdc2..de87a7c 100644 --- a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java +++ b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java @@ -33,9 +33,11 @@ public static void main(String[] args) { public static void addStudent(Map map, String name, int grade) { // TODO: Implement this method - + map.put(name, grade); } + + /* Problem 2 Update the grade of an existing student. @@ -47,9 +49,11 @@ 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); } + + /* Problem 3 Remove a student from the LinkedHashMap. @@ -61,9 +65,11 @@ 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); } + + /* Problem 4 Return the first student inserted into the LinkedHashMap. @@ -75,10 +81,11 @@ public static void removeStudent(Map map, String name) { public static String getFirstInserted(Map map) { // TODO: Implement this method - - return null; + return map.keySet().iterator().next(); } + + /* Problem 5 Given a list of words, return a LinkedHashMap that counts @@ -91,7 +98,14 @@ public static String getFirstInserted(Map map) { public static Map wordFrequency(List words) { // TODO: Implement this method + Map map = new LinkedHashMap<>(); - return null; + for (String word : words) { + map.put(word, map.getOrDefault(word, 0) + 1); + } + + return map; } + + } diff --git a/src/main/java/Maps/TreeMap/TreeMapProblems.java b/src/main/java/Maps/TreeMap/TreeMapProblems.java index b101d2e..beb28ce 100644 --- a/src/main/java/Maps/TreeMap/TreeMapProblems.java +++ b/src/main/java/Maps/TreeMap/TreeMapProblems.java @@ -31,9 +31,11 @@ public static void main(String[] args) { public static void addPlayer(TreeMap map, int rank, String name) { // TODO: Implement this method - + map.put(rank, name); } + + /* Problem 2 Return the player with the highest ranking (smallest key). @@ -45,10 +47,11 @@ Return the player with the highest ranking (smallest key). public static String getTopPlayer(TreeMap map) { // TODO: Implement this method - - return null; + return map.get(map.firstKey()); } + + /* Problem 3 Return the player with the lowest ranking (largest key). @@ -60,10 +63,10 @@ Return the player with the lowest ranking (largest key). public static String getLowestPlayer(TreeMap map) { // TODO: Implement this method - - return null; + return map.get(map.lastKey()); } + /* Problem 4 Remove a player based on their rank. @@ -75,9 +78,11 @@ public static String getLowestPlayer(TreeMap map) { public static void removePlayer(TreeMap map, int rank) { // TODO: Implement this method - + map.remove(rank); } + + /* Problem 5 Return the next higher rank after the given rank. @@ -89,7 +94,8 @@ public static void removePlayer(TreeMap map, int rank) { public static Integer getNextRank(TreeMap map, int rank) { // TODO: Implement this method - - return null; + return map.higherKey(rank); } + + } diff --git a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java index dac03cf..7daa68c 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -31,9 +31,11 @@ public static void main(String[] args) { public static void addToFront(ArrayDeque deque, int value) { // TODO: Implement this method - + deque.addFirst(value); } + + /* Problem 2 Add a number to the BACK of the deque. @@ -45,9 +47,11 @@ public static void addToFront(ArrayDeque deque, int value) { public static void addToBack(ArrayDeque deque, int value) { // TODO: Implement this method - + deque.addLast(value); } + + /* Problem 3 Remove the element at the FRONT of the deque. @@ -59,9 +63,11 @@ public static void addToBack(ArrayDeque deque, int value) { public static void removeFront(ArrayDeque deque) { // TODO: Implement this method - + deque.removeFirst(); } + + /* Problem 4 Remove the element at the BACK of the deque. @@ -73,9 +79,11 @@ public static void removeFront(ArrayDeque deque) { public static void removeBack(ArrayDeque deque) { // TODO: Implement this method - + deque.removeLast(); } + + /* Problem 5 Return the FIRST element in the deque without removing it. @@ -87,10 +95,11 @@ public static void removeBack(ArrayDeque deque) { public static Integer peekFront(ArrayDeque deque) { // TODO: Implement this method - - return null; + return deque.peekFirst(); } + + /* Problem 6 Return the LAST element in the deque without removing it. @@ -102,7 +111,8 @@ public static Integer peekFront(ArrayDeque deque) { public static Integer peekBack(ArrayDeque deque) { // TODO: Implement this method - - return null; + return deque.peekLast(); } + + } diff --git a/src/main/java/Queues/Deque/DequeProblems.java b/src/main/java/Queues/Deque/DequeProblems.java index 7ef0c06..bff87a3 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -1,14 +1,23 @@ package Queues.Deque; import java.util.Deque; +import java.util.ArrayDeque; + public class DequeProblems { public static void main(String[] args) { // You can test your methods here + Deque deque = new ArrayDeque<>(); + + addFront(deque, 5); + addBack(deque, 10); + + System.out.println(deque); } + /* Problem 1 Add a value to the FRONT of the deque. @@ -20,9 +29,11 @@ public static void main(String[] args) { public static void addFront(Deque deque, int value) { // TODO: Implement this method + deque.addFirst(value); } + /* Problem 2 Add a value to the BACK of the deque. @@ -34,9 +45,11 @@ public static void addFront(Deque deque, int value) { public static void addBack(Deque deque, int value) { // TODO: Implement this method + deque.addLast(value); } + /* Problem 3 Remove and return the FRONT element of the deque. @@ -48,10 +61,13 @@ public static void addBack(Deque deque, int value) { public static Integer removeFront(Deque deque) { // TODO: Implement this method + deque.removeFirst(); + return deque.removeFirst(); - return null; } + + /* Problem 4 Remove and return the BACK element of the deque. @@ -63,10 +79,13 @@ public static Integer removeFront(Deque deque) { public static Integer removeBack(Deque deque) { // TODO: Implement this method + deque.removeLast(); + return deque.removeLast(); - return null; } + + /* Problem 5 Return the FRONT element without removing it. @@ -78,10 +97,12 @@ public static Integer removeBack(Deque deque) { public static Integer peekFront(Deque deque) { // TODO: Implement this method + return deque.peekFirst(); - return null; } + + /* Problem 6 Return the BACK element without removing it. @@ -93,8 +114,10 @@ public static Integer peekFront(Deque deque) { public static Integer peekBack(Deque deque) { // TODO: Implement this method + return deque.peekLast(); - return null; } + } + diff --git a/src/main/java/Sets/HashSet/HashSetProblems.java b/src/main/java/Sets/HashSet/HashSetProblems.java index a36c570..6fad1ce 100644 --- a/src/main/java/Sets/HashSet/HashSetProblems.java +++ b/src/main/java/Sets/HashSet/HashSetProblems.java @@ -2,14 +2,22 @@ import java.util.List; import java.util.Set; +import java.util.HashSet; public class HashSetProblems { public static void main(String[] args) { // You can test your methods here + Set set = new HashSet<>(); + + addElement(set, "apple"); + + System.out.println(set); // Output: [apple] } + + /* Problem 1 Add an element to the set. @@ -21,9 +29,11 @@ public static void main(String[] args) { public static void addElement(Set set, String value) { // TODO: Implement this method - + set.add(value); } + + /* Problem 2 Check if the set contains a value. @@ -35,10 +45,12 @@ public static void addElement(Set set, String value) { public static boolean containsValue(Set set, String value) { // TODO: Implement this method + return set.contains(value); - return false; } + + /* Problem 3 Remove a value from the set. @@ -50,9 +62,12 @@ public static boolean containsValue(Set set, String value) { public static void removeValue(Set set, String value) { // TODO: Implement this method + set.remove(value); } + + /* Problem 4 Return the number of unique elements in the set. @@ -64,10 +79,12 @@ public static void removeValue(Set set, String value) { public static int getUniqueCount(Set set) { // TODO: Implement this method + return set.size(); - return 0; } + + /* Problem 5 Given a list of integers, return a HashSet containing only the unique values. @@ -79,7 +96,9 @@ public static int getUniqueCount(Set set) { public static Set getUniqueValues(List numbers) { // TODO: Implement this method - - return null; + return new HashSet<>(numbers); } + + + }