diff --git a/src/main/java/Collections/Practice/CollectionBasics.java b/src/main/java/Collections/Practice/CollectionBasics.java index e45cb49..ffbd9a1 100644 --- a/src/main/java/Collections/Practice/CollectionBasics.java +++ b/src/main/java/Collections/Practice/CollectionBasics.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; public class CollectionBasics { public static void main(String[] args) { @@ -21,7 +22,6 @@ public static void main(String[] args) { System.out.println("Contains duplicates? " + hasDuplicates(numbers)); } - /* PROBLEM 1 Return the sum of all numbers in the collection @@ -30,14 +30,13 @@ public static int sum(Collection numbers) { int total = 0; - // TODO: - // Loop through the collection - // Add each number to total + for (Integer num : numbers) { + total += num; + } return total; } - /* PROBLEM 2 Count how many numbers are even @@ -46,14 +45,15 @@ public static int countEven(Collection numbers) { int count = 0; - // TODO: - // Loop through the collection - // If the number is even, increase count + for (Integer num : numbers) { + if (num % 2 == 0) { + count++; + } + } return count; } - /* PROBLEM 3 Find the largest number in the collection @@ -62,14 +62,15 @@ public static int findMax(Collection numbers) { int max = Integer.MIN_VALUE; - // TODO: - // Loop through numbers - // Update max if current number is larger + for (Integer num : numbers) { + if (num > max) { + max = num; + } + } return max; } - /* PROBLEM 4 Return true if the collection contains duplicates @@ -77,14 +78,10 @@ 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 numbers.size() != new HashSet<>(numbers).size(); - return false; } - /* PROBLEM 5 Count how many times a target value appears @@ -93,14 +90,15 @@ public static int countOccurrences(Collection numbers, int target) { int count = 0; - // TODO: - // Loop through numbers - // If number equals target, increase count + for (Integer num : numbers) { + if (num == target) { + count++; + } + } return count; } - /* BONUS PROBLEM Create and return a new collection @@ -110,10 +108,12 @@ public static Collection filterGreaterThanTwenty(Collection nu Collection result = new ArrayList<>(); - // TODO: - // Loop through numbers - // Add numbers greater than 20 to result + for (Integer num : numbers) { + if (num > 20) { + result.add(num); + } + } return result; } -} +} \ No newline at end of file diff --git a/src/main/java/Collections/Quiz/KnowledgeCheck.md b/src/main/java/Collections/Quiz/KnowledgeCheck.md index 43ee11f..6b3dd31 100644 --- a/src/main/java/Collections/Quiz/KnowledgeCheck.md +++ b/src/main/java/Collections/Quiz/KnowledgeCheck.md @@ -16,7 +16,7 @@ A. `Collection` is a class B. `Collection` extends `List` C. `Collection` is the root interface for most collection types D. `Collection` only stores key-value pairs - +C --- ## Question 2 @@ -36,7 +36,7 @@ A. 0 B. 1 C. 3 D. 30 - +C --- ## Question 3 @@ -47,7 +47,7 @@ A. `search()` B. `exists()` C. `contains()` D. `check()` - +C --- ## Question 4 @@ -69,7 +69,7 @@ A. `[Alex, Jordan]` B. `[Jordan]` C. `[Alex]` D. `[]` - +B --- ## Question 5 @@ -80,7 +80,7 @@ A. `add()` B. `addAll()` C. `insertAll()` D. `merge()` - +B --- ## Question 6 @@ -97,7 +97,7 @@ A. `true` B. `false` C. `0` D. Compilation error - +A --- ## Question 7 @@ -108,7 +108,7 @@ A. `removeAll()` B. `deleteAll()` C. `clear()` D. `reset()` - +C --- ## Question 8 @@ -131,7 +131,7 @@ A. `5 10 15` B. `15 10 5` C. `0 1 2` D. Compilation error - +A --- ## Question 9 @@ -142,7 +142,7 @@ A. Elements can be added B. Elements can be removed C. Indexed access using `.get(index)` D. Elements can be iterated using a loop - +C --- ## Question 10 @@ -157,6 +157,6 @@ A. It automatically sorts the collection B. It prevents duplicates C. It allows flexibility to change implementations later D. It forces the collection to be synchronized - +C ``` ``` diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java index 6c2f4e1..e884d3e 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -1,178 +1,145 @@ package CollectionsHackerrank; -import java.util.List; -import java.util.Map; -import java.util.Queue; +import java.util.*; public class CollectionsHackerrankProblems { - public class CollectionsHackerrankPractice { + public static class CollectionsHackerrankPractice { public static void main(String[] args) { - - // You can test your methods here - + // Example usage and testing can go here } /* Problem 1 Remove duplicates from a list of integers. - - Example - Input: [1,2,2,3,4,4,5] - Output: [1,2,3,4,5] */ public static List removeDuplicates(List numbers) { - - // TODO: Implement this method - - return null; + return new ArrayList<>(new LinkedHashSet<>(numbers)); } /* Problem 2 Count how many times each number appears. - - Example - Input: [1,2,2,3,3,3] - Output: {1=1, 2=2, 3=3} */ public static Map countFrequency(List numbers) { - - // TODO: Implement this method - - return null; + Map freqMap = new HashMap<>(); + for (int num : numbers) { + freqMap.put(num, freqMap.getOrDefault(num, 0) + 1); + } + return freqMap; } /* Problem 3 Return the first number that appears only once. - - Example - Input: [4,5,1,2,0,4] - Output: 5 */ public static Integer firstUnique(List numbers) { - - // TODO: Implement this method - + Map freqMap = countFrequency(numbers); + for (int num : numbers) { + if (freqMap.get(num) == 1) return num; + } return null; } /* Problem 4 Return true if any two numbers add up to the target. - - Example - numbers = [2,7,11,15] - target = 9 - - Output: true */ 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; } /* Problem 5 Count how many unique words exist in a list. - - Example - Input: ["apple","banana","apple","orange"] - Output: 3 */ public static int countUniqueWords(List words) { - - // TODO: Implement this method - - return 0; + return new HashSet<>(words).size(); } /* Problem 6 Reverse a queue. - - Example - Input: [1,2,3,4] - 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.poll()); + while (!stack.isEmpty()) queue.add(stack.pop()); + return queue; } /* Problem 7 Determine whether parentheses are balanced. - - Example - Input: "(())" - Output: true - - Input: "(()" - Output: false */ 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); + } 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. - - Example - Input: [1,3,2,3,4,3] - Output: 3 */ public static Integer mostFrequent(List numbers) { - - // TODO: Implement this method - - return null; + Map freqMap = countFrequency(numbers); + int maxCount = 0; + Integer mostFreq = null; + for (Map.Entry entry : freqMap.entrySet()) { + if (entry.getValue() > maxCount) { + maxCount = entry.getValue(); + mostFreq = entry.getKey(); + } + } + return mostFreq; } /* Problem 9 Group words based on their length. - - Example - Input: ["cat","dog","elephant","ant"] - - Output: - { - 3 = ["cat","dog","ant"], - 8 = ["elephant"] - } */ public static Map> groupByLength(List words) { - - // TODO: Implement this method - - return null; + Map> map = new HashMap<>(); + for (String word : words) { + int len = word.length(); + map.computeIfAbsent(len, k -> new ArrayList<>()).add(word); + } + return map; } /* Problem 10 Return the maximum sum of any window of size k. - - Example - numbers = [2,1,5,1,3,2] - k = 3 - - Output: 9 */ public static int maxSlidingWindowSum(List numbers, int k) { - - // TODO: Implement this method - - return 0; + if (numbers == null || numbers.size() < k) return 0; + int maxSum = 0; + int windowSum = 0; + + // initial window sum + for (int i = 0; i < k; i++) { + windowSum += numbers.get(i); + } + maxSum = windowSum; + + for (int i = k; i < numbers.size(); i++) { + windowSum += numbers.get(i) - numbers.get(i - k); + maxSum = Math.max(maxSum, windowSum); + } + return maxSum; } } -} +} \ No newline at end of file diff --git a/src/main/java/Iterable/Examples/ForEachLoopDemo.java b/src/main/java/Iterable/Examples/ForEachLoopDemo.java index a6d2198..bd3f879 100644 --- a/src/main/java/Iterable/Examples/ForEachLoopDemo.java +++ b/src/main/java/Iterable/Examples/ForEachLoopDemo.java @@ -15,23 +15,27 @@ public static void main(String[] args) { System.out.println("Printing students using a for-each loop:"); - // TODO: - // Use a for-each loop to print each student name - + // 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 - + // Print each name in uppercase + for(String student : students) { + System.out.println(student.toUpperCase()); + } System.out.println("\nCount the number of students:"); int count = 0; - // TODO: - // Use a for-each loop to count how many students are in the list + // Count students + for(String student : students) { + count++; + } System.out.println("Total students: " + count); } -} +} \ No newline at end of file diff --git a/src/main/java/Iterable/Examples/IteratorDemo.java b/src/main/java/Iterable/Examples/IteratorDemo.java index b09fca7..35e9464 100644 --- a/src/main/java/Iterable/Examples/IteratorDemo.java +++ b/src/main/java/Iterable/Examples/IteratorDemo.java @@ -23,21 +23,26 @@ public static void main(String[] args) { System.out.println("\nIterating using Iterator:"); - // TODO: - // Use iterator.hasNext() and iterator.next() - // Print each number - + // Iterate through list + while(iterator.hasNext()) { + Integer num = iterator.next(); + System.out.println(num); + } System.out.println("\nRemoving odd numbers using Iterator"); iterator = numbers.iterator(); - // TODO: - // Use iterator to remove odd numbers - // Remember: use iterator.remove() + // Remove odd numbers + while(iterator.hasNext()) { + Integer num = iterator.next(); + if(num % 2 != 0) { + iterator.remove(); + } + } System.out.println("\nUpdated list:"); System.out.println(numbers); } -} +} \ No newline at end of file diff --git a/src/main/java/Iterable/Practice/IterableWarmups.java b/src/main/java/Iterable/Practice/IterableWarmups.java index 9e9de94..59b6a88 100644 --- a/src/main/java/Iterable/Practice/IterableWarmups.java +++ b/src/main/java/Iterable/Practice/IterableWarmups.java @@ -19,66 +19,69 @@ public static void main(String[] args) { System.out.println("Max value: " + findMax(numbers)); } - /* PROBLEM 1 Return the sum of all numbers in the iterable - */ + */ public static int sum(Iterable numbers) { int total = 0; - // TODO: - // Use a for-each loop to calculate the sum + for (Integer num : numbers) { + total += num; + } return total; } - /* PROBLEM 2 Count how many numbers are even - */ + */ public static int countEven(Iterable numbers) { int count = 0; - // 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 - */ + */ public static int findMax(Iterable numbers) { int max = Integer.MIN_VALUE; - // 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 - */ + */ public static int countMatches(Iterable words, String target) { int count = 0; - // TODO: - // Loop through words - // Compare each word to target + for (String word : words) { + if (word.equals(target)) { + count++; + } + } return count; } -} +} \ No newline at end of file diff --git a/src/main/java/Lists/ArrayLists/ArrayListProblems.java b/src/main/java/Lists/ArrayLists/ArrayListProblems.java index baf4dfa..cfe681a 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -1,8 +1,9 @@ package Lists.ArrayLists; - import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; public class ArrayListProblems { public static void main(String[] args) { @@ -26,80 +27,69 @@ public static void main(String[] args) { /* Problem 1 Return the sum of all numbers in the list. - - Example - Input: [1,2,3] - Output: 6 */ public static int sum(List nums) { - - // TODO: Implement this method - - return 0; + int total = 0; + for (int num : nums) { + total += num; + } + return total; } /* Problem 2 Count how many EVEN numbers exist in the list. - - Example - Input: [1,2,4,7] - Output: 2 */ public static int countEvens(List nums) { - - // TODO: Implement this method - - return 0; + int count = 0; + for (int num : nums) { + if (num % 2 == 0) { + count++; + } + } + return count; } /* Problem 3 Determine if the list contains any duplicate values. - - Example - Input: [1,2,3,1] - Output: true - - Input: [1,2,3] - Output: false */ public static boolean hasDuplicate(List nums) { - - // TODO: Implement this method - + Set seen = new HashSet<>(); + for (int num : nums) { + if (!seen.add(num)) { + return true; // duplicate found + } + } return false; } /* Problem 4 Return the largest number in the list. - - Example - Input: [4,2,7] - Output: 7 */ public static int findMax(List nums) { - - // TODO: Implement this method - - return 0; + if (nums.isEmpty()) { + throw new RuntimeException("List is empty"); + } + int max = nums.get(0); + for (int num : nums) { + if (num > max) { + max = num; + } + } + return max; } /* Problem 5 Return a NEW list that contains the elements of the original list in reverse order. - - Example - Input: [1,2,3] - Output: [3,2,1] - - The original list should remain unchanged. */ 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; } -} +} \ No newline at end of file diff --git a/src/main/java/Lists/LinkedLists/LinkedListProblems.java b/src/main/java/Lists/LinkedLists/LinkedListProblems.java index 178a2ba..9ee9749 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -30,86 +30,58 @@ public static void main(String[] args) { /* Problem 1 Add a value to the FRONT of the LinkedList. - - Example - Input: [10,20,30], value=5 - Output: [5,10,20,30] */ public static void addToFront(LinkedList list, int value) { - - // TODO: Implement this method - + list.addFirst(value); } /* Problem 2 Add a value to the END of the LinkedList. - - Example - Input: [10,20,30], value=40 - Output: [10,20,30,40] */ public static void addToEnd(LinkedList list, int value) { - - // TODO: Implement this method - + list.addLast(value); } /* Problem 3 Remove the FIRST element from the LinkedList. - - Example - Input: [10,20,30] - Output: [20,30] */ public static void removeFirstElement(LinkedList list) { - - // TODO: Implement this method - + if(!list.isEmpty()) { + list.removeFirst(); + } } /* Problem 4 Remove the LAST element from the LinkedList. - - Example - Input: [10,20,30] - Output: [10,20] */ public static void removeLastElement(LinkedList list) { - - // TODO: Implement this method - + if(!list.isEmpty()) { + list.removeLast(); + } } /* Problem 5 Return the FIRST element in the LinkedList. - - Example - Input: [10,20,30] - Output: 10 */ public static int getFirstElement(LinkedList list) { - - // TODO: Implement this method - - return 0; + if(!list.isEmpty()) { + return list.getFirst(); + } + throw new RuntimeException("List is empty"); } /* Problem 6 Return the LAST element in the LinkedList. - - Example - Input: [10,20,30] - Output: 30 */ public static int getLastElement(LinkedList list) { - - // TODO: Implement this method - - return 0; + if(!list.isEmpty()) { + return list.getLast(); + } + throw new RuntimeException("List is empty"); } -} +} \ No newline at end of file diff --git a/src/main/java/Maps/HashMap/HashMapProblems.java b/src/main/java/Maps/HashMap/HashMapProblems.java index 04b5567..6e1c886 100644 --- a/src/main/java/Maps/HashMap/HashMapProblems.java +++ b/src/main/java/Maps/HashMap/HashMapProblems.java @@ -22,77 +22,54 @@ public static void main(String[] args) { removeItem(inventory, "Oranges"); System.out.println("After Removal: " + inventory); + + List numbers = List.of(1, 2, 2, 3, 3, 3); + System.out.println("Frequency count: " + countFrequency(numbers)); } /* Problem 1 Add an item and its quantity to the map. - - Example - Input: ("Apples", 10) - Output: {"Apples"=10} */ 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. - - Example - Input: ("Apples") - Output: 10 */ public static int getQuantity(Map map, String item) { - - // TODO: Implement this method - - return 0; + return map.getOrDefault(item, 0); // returns 0 if item does not exist } /* Problem 3 Update the quantity of an existing item. - - Example - Input: ("Bananas", 12) - Output: {"Bananas"=12} */ public static void updateQuantity(Map map, String item, int newQuantity) { - - // TODO: Implement this method - + if(map.containsKey(item)) { + map.put(item, newQuantity); + } } /* Problem 4 Remove an item from the map. - - Example - Input: ("Oranges") - Output: item removed */ 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. - - Example - Input: [1,2,2,3,3,3] - Output: {1=1, 2=2, 3=3} */ public static Map countFrequency(List numbers) { - - // TODO: Implement this method - - return null; + Map freqMap = new HashMap<>(); + for(Integer num : numbers) { + freqMap.put(num, freqMap.getOrDefault(num, 0) + 1); + } + return freqMap; } -} +} \ No newline at end of file diff --git a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java index e8bbdc2..3ca5c96 100644 --- a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java +++ b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java @@ -20,78 +20,57 @@ public static void main(String[] args) { removeStudent(studentGrades, "Morgan"); System.out.println("After Removal: " + studentGrades); + + System.out.println("First inserted student: " + getFirstInserted(studentGrades)); + + List words = List.of("apple","banana","apple","orange"); + System.out.println("Word Frequencies: " + wordFrequency(words)); } /* Problem 1 Add a student and their grade to the LinkedHashMap. - - Example - Input: ("Jordan", 90) - Output: {"Jordan"=90} */ 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. - - Example - Input: ("Taylor", 92) - Output: {"Taylor"=92} */ public static void updateGrade(Map map, String name, int newGrade) { - - // TODO: Implement this method - + if(map.containsKey(name)) { + map.put(name, newGrade); + } } /* Problem 3 Remove a student from the LinkedHashMap. - - Example - Input: ("Morgan") - Output: Student removed from map */ 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. - - Example - Input: {"Jordan"=90, "Taylor"=85} - Output: "Jordan" */ public static String getFirstInserted(Map map) { - - // TODO: Implement this method - - return null; + if(map.isEmpty()) return null; + return map.keySet().iterator().next(); } /* Problem 5 - Given a list of words, return a LinkedHashMap that counts - how many times each word appears while preserving insertion order. - - Example - Input: ["apple","banana","apple","orange"] - Output: {apple=2, banana=1, orange=1} + Return a LinkedHashMap counting word frequencies while preserving insertion order. */ public static Map wordFrequency(List words) { - - // TODO: Implement this method - - return null; + Map freqMap = new LinkedHashMap<>(); + for(String word : words) { + freqMap.put(word, freqMap.getOrDefault(word, 0) + 1); + } + return freqMap; } -} +} \ No newline at end of file diff --git a/src/main/java/Maps/TreeMap/TreeMapProblems.java b/src/main/java/Maps/TreeMap/TreeMapProblems.java index b101d2e..c8379aa 100644 --- a/src/main/java/Maps/TreeMap/TreeMapProblems.java +++ b/src/main/java/Maps/TreeMap/TreeMapProblems.java @@ -18,78 +18,49 @@ 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)); } /* Problem 1 Add a player to the TreeMap with their rank. - - Example - Input: (1, "Jordan") - Output: {1="Jordan"} */ 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). - - Example - Input: {1="Jordan", 2="Taylor"} - Output: "Jordan" */ public static String getTopPlayer(TreeMap map) { - - // TODO: Implement this method - - return null; + if (map.isEmpty()) return null; + return map.firstEntry().getValue(); } /* Problem 3 Return the player with the lowest ranking (largest key). - - Example - Input: {1="Jordan", 2="Taylor"} - Output: "Taylor" */ public static String getLowestPlayer(TreeMap map) { - - // TODO: Implement this method - - return null; + if (map.isEmpty()) return null; + return map.lastEntry().getValue(); } /* Problem 4 Remove a player based on their rank. - - Example - Input: remove rank 2 - Output: player removed */ 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. - - Example - Input: rank=2, map={1="A",2="B",3="C"} - Output: 3 */ public static Integer getNextRank(TreeMap map, int rank) { - - // TODO: Implement this method - - return null; + return map.higherKey(rank); // returns null if no higher key exists } -} +} \ No newline at end of file diff --git a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java index dac03cf..6514543 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -28,10 +28,11 @@ public static void main(String[] args) { Input: value = 5 Output: [5] */ + public static void addToFront(ArrayDeque deque, int value) { // TODO: Implement this method - + deque.addFirst(value); } /* @@ -42,10 +43,11 @@ public static void addToFront(ArrayDeque deque, int value) { Input: value = 10 Output: [5,10] */ + public static void addToBack(ArrayDeque deque, int value) { // TODO: Implement this method - + deque.addLast(value); } /* @@ -59,6 +61,9 @@ public static void addToBack(ArrayDeque deque, int value) { public static void removeFront(ArrayDeque deque) { // TODO: Implement this method + if (!deque.isEmpty()) { + deque.removeFirst(); + } } @@ -73,6 +78,9 @@ public static void removeFront(ArrayDeque deque) { public static void removeBack(ArrayDeque deque) { // TODO: Implement this method + if (!deque.isEmpty()) { + deque.removeLast(); + } } @@ -88,7 +96,8 @@ public static Integer peekFront(ArrayDeque deque) { // TODO: Implement this method - return null; + + return deque.peekFirst(); } /* @@ -103,6 +112,6 @@ 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..8f40f43 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -20,6 +20,8 @@ public static void main(String[] args) { public static void addFront(Deque deque, int value) { // TODO: Implement this method + deque.addFirst(value); + } @@ -34,6 +36,7 @@ public static void addFront(Deque deque, int value) { public static void addBack(Deque deque, int value) { // TODO: Implement this method + deque.addLast(value); } @@ -49,7 +52,8 @@ public static Integer removeFront(Deque deque) { // TODO: Implement this method - return null; + return deque.pollFirst(); + } /* @@ -63,8 +67,8 @@ public static Integer removeFront(Deque deque) { public static Integer removeBack(Deque deque) { // TODO: Implement this method + return deque.pollLast(); - return null; } /* @@ -78,8 +82,8 @@ public static Integer removeBack(Deque deque) { public static Integer peekFront(Deque deque) { // TODO: Implement this method + return deque.peekFirst(); - return null; } /* @@ -94,7 +98,7 @@ public static Integer peekBack(Deque deque) { // TODO: Implement this method - return null; + return deque.peekLast(); } } diff --git a/src/main/java/Sets/HashSet/HashSetProblems.java b/src/main/java/Sets/HashSet/HashSetProblems.java index a36c570..25afd23 100644 --- a/src/main/java/Sets/HashSet/HashSetProblems.java +++ b/src/main/java/Sets/HashSet/HashSetProblems.java @@ -2,6 +2,7 @@ import java.util.List; import java.util.Set; +import java.util.HashSet; public class HashSetProblems { public static void main(String[] args) { @@ -13,73 +14,51 @@ public static void main(String[] args) { /* Problem 1 Add an element to the set. - - Example - Input: "apple" - Output: {"apple"} */ public static void addElement(Set set, String value) { - // TODO: Implement this method + set.add(value); } /* Problem 2 Check if the set contains a value. - - Example - Input: "banana" - Output: true or false */ 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. - - Example - Input: "apple" - Output: value removed */ 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. - - Example - Input: {"apple","banana","apple"} - Output: 2 */ 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. - - Example - Input: [1,2,2,3,3,3] - Output: {1,2,3} */ public static Set getUniqueValues(List numbers) { - // TODO: Implement this method + Set uniqueValues = new HashSet<>(numbers); + return uniqueValues; - return null; } -} +} \ No newline at end of file