diff --git a/src/main/java/Collections/Practice/CollectionBasics.java b/src/main/java/Collections/Practice/CollectionBasics.java index e45cb49..48d6457 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.Set; public class CollectionBasics { public static void main(String[] args) { @@ -34,6 +35,9 @@ public static int sum(Collection numbers) { // Loop through the collection // Add each number to total + for (int num : numbers) { + total += num; + } return total; } @@ -49,7 +53,11 @@ public static int countEven(Collection numbers) { // TODO: // Loop through the collection // If the number is even, increase count - + for (int num : numbers) { + if (num % 2 == 0) { + count++; + } + } return count; } @@ -65,7 +73,11 @@ public static int findMax(Collection numbers) { // TODO: // Loop through numbers // Update max if current number is larger - + for (int num : numbers) { + if (num > max) { + max = num; + } + } return max; } @@ -80,7 +92,9 @@ public static boolean hasDuplicates(Collection numbers) { // TODO: // Hint: // Compare the size of a collection with the size of a Set - + if (numbers.size() != Set.copyOf(numbers).size()) { + return true; + } return false; } @@ -96,6 +110,11 @@ public static int countOccurrences(Collection numbers, int target) { // TODO: // Loop through numbers // If number equals target, increase count + for (int num : numbers) { + if (num == target) { + count++; + } + } return count; } @@ -113,6 +132,11 @@ public static Collection filterGreaterThanTwenty(Collection nu // TODO: // Loop through numbers // Add numbers greater than 20 to result + for (int num : numbers) { + 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..7af1a60 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -1,178 +1,288 @@ 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 void main(String[] args) { - - // You can test your methods here - - } - - /* - Problem 1 - Remove duplicates from a list of integers. + public static void main(String[] args) { + + // Problem 1 + System.out.println("Problem 1 - Remove Duplicates"); + System.out.println(removeDuplicates(new ArrayList<>(List.of(1, 2, 2, 3, 4, 4, 5)))); + // Expected: [1,2,3,4,5] + + // Problem 2 + System.out.println("\nProblem 2 - Count Frequency"); + System.out.println(countFrequency(new ArrayList<>(List.of(1, 2, 2, 3, 3, 3)))); + // Expected: {1=1, 2=2, 3=3} + + // Problem 3 + System.out.println("\nProblem 3 - First Unique"); + System.out.println(firstUnique(new ArrayList<>(List.of(4, 5, 1, 2, 0, 4)))); + // Expected: 5 + + // Problem 4 + System.out.println("\nProblem 4 - Two Sum"); + System.out.println(twoSum(new ArrayList<>(List.of(2, 7, 11, 15)), 9)); + System.out.println(twoSum(new ArrayList<>(List.of(2, 8, 11, 15)), 9)); + // Expected: true, false + + // Problem 5 + System.out.println("\nProblem 5 - Count Unique Words"); + System.out.println(countUniqueWords(new ArrayList<>(List.of("apple", "banana", "apple", "orange")))); + // Expected: 3 + + // Problem 6 + System.out.println("\nProblem 6 - Reverse Queue"); + Queue queue = new LinkedList<>(List.of(1, 2, 3, 4)); + System.out.println(reverseQueue(queue)); + // Expected: [4,3,2,1] + + // Problem 7 + System.out.println("\nProblem 7 - Balanced Parentheses"); + System.out.println(isBalanced("(())")); + // Expected: true + System.out.println(isBalanced("(()")); + // Expected: false + + // Problem 8 + System.out.println("\nProblem 8 - Most Frequent"); + System.out.println(mostFrequent(new ArrayList<>(List.of(1, 3, 2, 3, 4, 3)))); + // Expected: 3 + + // Problem 9 + System.out.println("\nProblem 9 - Group By Length"); + System.out.println(groupByLength(new ArrayList<>(List.of("cat", "dog", "elephant", "ant")))); + // Expected: {3=[cat,dog,ant], 8=[elephant]} + + // Problem 10 + System.out.println("\nProblem 10 - Max Sliding Window Sum"); + System.out.println(maxSlidingWindowSum(new ArrayList<>(List.of(2, 1, 5, 1, 3, 2)), 3)); + // Expected: 9 + } - Example - Input: [1,2,2,3,4,4,5] - Output: [1,2,3,4,5] - */ - public static List removeDuplicates(List numbers) { + /* + Problem 1 + Remove duplicates from a list of integers. - // TODO: Implement this method + Example + Input: [1,2,2,3,4,4,5] + Output: [1,2,3,4,5] + */ + public static List removeDuplicates(List numbers) { + return new ArrayList<>(new LinkedHashSet<>(numbers)); + } - return null; + /* + 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) { + Map answer = new HashMap<>(); + for (Integer num : numbers) { + if (answer.containsKey(num)) { + int value = answer.get(num); + answer.replace(num, value + 1); + } else { + answer.put(num, 1); + } } + return answer; + } - /* - 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; + /* + 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) { + LinkedHashMap answer = new LinkedHashMap<>(); + for (Integer num : numbers) { + if (answer.containsKey(num)) { + int value = answer.get(num); + answer.replace(num, value + 1); + } else { + answer.put(num, 1); + } } - - /* - 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 - - return null; + for (Integer key : answer.keySet()) { + if (answer.get(key) == 1) { + return key; + } } + return 0; + } - /* - 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 - - return false; + /* + 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) { + for (int i = 0; i < numbers.size() - 1; i++) { + for (int j = i + 1; j < numbers.size(); j++) { + if (numbers.get(i) + numbers.get(j) == target) { + return true; + } + } } + 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 + /* + 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) { + Set unique = new HashSet<>(new HashSet<>(words)); + return unique.size(); + } - return 0; + /* + Problem 6 + Reverse a queue. + + Example + Input: [1,2,3,4] + Output: [4,3,2,1] + */ + public static Queue reverseQueue(Queue queue) { + Deque answer = new ArrayDeque<>(); + while (!queue.isEmpty()) { + answer.addFirst(queue.remove()); } + return answer; + } - /* - 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; + /* + Problem 7 + Determine whether parentheses are balanced. + + Example + Input: "(())" ()) [ { + Output: true + + Input: "(()" + Output: false + */ + public static boolean isBalanced(String expression) { + Stack stack = new Stack<>(); + + for (int i = 0; i < expression.length(); i++) { + char current = expression.charAt(i); + + if (current == '(') { + stack.push(current); + } else if (current == ')') { + if (stack.isEmpty()) { + return false; + } + stack.pop(); + } } + return stack.isEmpty(); + } - /* - 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; + /* + 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) { + HashMap numberCount = new HashMap<>(); + int max = Integer.MIN_VALUE; + int answer = 0; + for (Integer num : numbers) { + if (numberCount.containsKey(num)) { + int value = numberCount.get(num); + numberCount.replace(num, value + 1); + } else { + numberCount.put(num, 1); + } } - /* - 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; + for (Integer key : numberCount.keySet()) { + int currentValue = numberCount.get(key); + if (numberCount.get(key) > max) { + max = currentValue; + answer = key; + } } + return answer; + } - /* - 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) { + /* + Problem 9 + Group words based on their length. - // TODO: Implement this method + Example + Input: ["cat","dog","elephant","ant"] - return null; + Output: + { + 3 = ["cat","dog","ant"], + 8 = ["elephant"] + } + */ + public static Map> groupByLength(List words) { + Map> answer = new HashMap<>(); + for (String word : words) { + int length = word.length(); + if (answer.containsKey(length)) { + List update = answer.get(length); + update.add(word); + answer.replace(length, update); + } else { + answer.put(length, new ArrayList<>(List.of(word))); + } } + return answer; + } - /* - 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; + /* + 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) { + int windowStart = 0; + int windowEnd = k; + int max = Integer.MIN_VALUE; + while (windowEnd != numbers.size()) { + int currentSum = 0; + for (int i = windowStart; i < windowEnd; i++) { + currentSum += numbers.get(i); + } + if (currentSum > max) { + max = currentSum; + } + windowStart++; + windowEnd++; } + return max; } } diff --git a/src/main/java/Iterable/Examples/ForEachLoopDemo.java b/src/main/java/Iterable/Examples/ForEachLoopDemo.java index a6d2198..88ef909 100644 --- a/src/main/java/Iterable/Examples/ForEachLoopDemo.java +++ b/src/main/java/Iterable/Examples/ForEachLoopDemo.java @@ -17,13 +17,18 @@ 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 +36,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..635267f 100644 --- a/src/main/java/Iterable/Examples/IteratorDemo.java +++ b/src/main/java/Iterable/Examples/IteratorDemo.java @@ -27,6 +27,11 @@ public static void main(String[] args) { // Use iterator.hasNext() and iterator.next() // Print each number + while (iterator.hasNext()) { + int num = iterator.next(); + System.out.println(num); + } + System.out.println("\nRemoving odd numbers using Iterator"); @@ -36,6 +41,13 @@ public static void main(String[] args) { // Use iterator to remove odd numbers // Remember: use iterator.remove() + while (iterator.hasNext()) { + int num = iterator.next(); + if (num % 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..459bc2a 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 num : numbers) { + total += num; + } return total; } @@ -46,7 +48,11 @@ public static int countEven(Iterable numbers) { // TODO: // Loop through numbers // Increment count if number is even - + for (int num : numbers) { + if (num % 2 == 0) { + count++; + } + } return count; } @@ -62,7 +68,11 @@ public static int findMax(Iterable numbers) { // TODO: // Loop through numbers // Update max if current number is larger - + for (int num : numbers) { + if (num > max) { + max = num; + } + } return max; } @@ -78,7 +88,11 @@ 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..8a3def9 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -3,10 +3,10 @@ import java.util.ArrayList; import java.util.List; +import java.util.Set; public class ArrayListProblems { public static void main(String[] args) { - List numbers = new ArrayList<>(); numbers.add(4); @@ -32,10 +32,11 @@ public static void main(String[] args) { Output: 6 */ public static int sum(List nums) { - - // TODO: Implement this method - - return 0; + int sum = 0; + for (int num : nums) { + sum += num; + } + return sum; } /* @@ -47,10 +48,13 @@ public static int sum(List nums) { Output: 2 */ public static int countEvens(List nums) { - - // TODO: Implement this method - - return 0; + int evens = 0; + for (int num : nums) { + if (num % 2 == 0) { + evens++; + } + } + return evens; } /* @@ -65,9 +69,9 @@ public static int countEvens(List nums) { Output: false */ public static boolean hasDuplicate(List nums) { - - // TODO: Implement this method - + if (nums.size() != Set.copyOf(nums).size()) { + return true; + } return false; } @@ -80,10 +84,13 @@ public static boolean hasDuplicate(List nums) { Output: 7 */ public static int findMax(List nums) { - - // TODO: Implement this method - - return 0; + int max = Integer.MIN_VALUE; + for (int num : nums) { + if (num > max) { + max = num; + } + } + return max; } /* @@ -97,9 +104,6 @@ public static int findMax(List nums) { The original list should remain unchanged. */ public static List reverse(List nums) { - - // TODO: Implement this method - - return null; + return nums.reversed(); } } diff --git a/src/main/java/Lists/LinkedLists/LinkedListProblems.java b/src/main/java/Lists/LinkedLists/LinkedListProblems.java index 178a2ba..3ab19a1 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -4,7 +4,6 @@ public class LinkedListProblems { public static void main(String[] args) { - LinkedList numbers = new LinkedList<>(); numbers.add(10); @@ -36,9 +35,7 @@ public static void main(String[] args) { Output: [5,10,20,30] */ public static void addToFront(LinkedList list, int value) { - - // TODO: Implement this method - + list.push(value); } /* @@ -50,8 +47,7 @@ public static void addToFront(LinkedList list, int value) { Output: [10,20,30,40] */ public static void addToEnd(LinkedList list, int value) { - - // TODO: Implement this method + list.addLast(value); } @@ -64,9 +60,7 @@ public static void addToEnd(LinkedList list, int value) { Output: [20,30] */ public static void removeFirstElement(LinkedList list) { - - // TODO: Implement this method - + list.removeFirst(); } /* @@ -78,9 +72,7 @@ public static void removeFirstElement(LinkedList list) { Output: [10,20] */ public static void removeLastElement(LinkedList list) { - - // TODO: Implement this method - + list.removeLast(); } /* @@ -92,10 +84,7 @@ public static void removeLastElement(LinkedList list) { Output: 10 */ public static int getFirstElement(LinkedList list) { - - // TODO: Implement this method - - return 0; + return list.getFirst(); } /* @@ -107,9 +96,6 @@ public static int getFirstElement(LinkedList list) { Output: 30 */ 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..5e59bfb 100644 --- a/src/main/java/Maps/HashMap/HashMapProblems.java +++ b/src/main/java/Maps/HashMap/HashMapProblems.java @@ -6,7 +6,6 @@ public class HashMapProblems { public static void main(String[] args) { - Map inventory = new HashMap<>(); addItem(inventory, "Apples", 10); @@ -33,9 +32,7 @@ public static void main(String[] args) { Output: {"Apples"=10} */ public static void addItem(Map map, String item, int quantity) { - - // TODO: Implement this method - + map.put(item, quantity); } /* @@ -47,10 +44,7 @@ public static void addItem(Map map, String item, int quantity) Output: 10 */ public static int getQuantity(Map map, String item) { - - // TODO: Implement this method - - return 0; + return map.get(item); } /* @@ -62,9 +56,7 @@ 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.replace(item, newQuantity); } /* @@ -76,9 +68,7 @@ public static void updateQuantity(Map map, String item, int new Output: item removed */ public static void removeItem(Map map, String item) { - - // TODO: Implement this method - + map.remove(item); } /* @@ -90,9 +80,15 @@ 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 answer = new HashMap<>(); + for (int num : numbers) { + if (answer.containsKey(num)) { + int value = answer.get(num); + answer.replace(num, value + 1); + } else { + answer.put(num, 1); + } + } + return answer; } } diff --git a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java index e8bbdc2..bf031e0 100644 --- a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java +++ b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java @@ -1,12 +1,12 @@ package Maps.LinkedHashMap; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class LinkedHashMapProblems { public static void main(String[] args) { - Map studentGrades = new LinkedHashMap<>(); addStudent(studentGrades, "Jordan", 90); @@ -31,9 +31,7 @@ public static void main(String[] args) { Output: {"Jordan"=90} */ public static void addStudent(Map map, String name, int grade) { - - // TODO: Implement this method - + map.put(name, grade); } /* @@ -45,9 +43,7 @@ public static void addStudent(Map map, String name, int grade) Output: {"Taylor"=92} */ public static void updateGrade(Map map, String name, int newGrade) { - - // TODO: Implement this method - + map.replace(name, newGrade); } /* @@ -59,9 +55,7 @@ public static void updateGrade(Map map, String name, int newGra Output: Student removed from map */ public static void removeStudent(Map map, String name) { - - // TODO: Implement this method - + map.remove(name); } /* @@ -73,9 +67,9 @@ public static void removeStudent(Map map, String name) { Output: "Jordan" */ public static String getFirstInserted(Map map) { - - // TODO: Implement this method - + for (String key : map.keySet()) { + return key; + } return null; } @@ -89,9 +83,15 @@ public static String getFirstInserted(Map map) { Output: {apple=2, banana=1, orange=1} */ public static Map wordFrequency(List words) { - - // TODO: Implement this method - - return null; + Map answer = new LinkedHashMap<>(); + for (String word : words) { + if (answer.containsKey(word)) { + int value = answer.get(word); + answer.replace(word, value + 1); + } else { + answer.put(word, 1); + } + } + return answer; } } diff --git a/src/main/java/Maps/TreeMap/TreeMapProblems.java b/src/main/java/Maps/TreeMap/TreeMapProblems.java index b101d2e..31f5e10 100644 --- a/src/main/java/Maps/TreeMap/TreeMapProblems.java +++ b/src/main/java/Maps/TreeMap/TreeMapProblems.java @@ -4,7 +4,6 @@ public class TreeMapProblems { public static void main(String[] args) { - TreeMap rankings = new TreeMap<>(); addPlayer(rankings, 3, "Jordan"); @@ -29,9 +28,7 @@ public static void main(String[] args) { Output: {1="Jordan"} */ public static void addPlayer(TreeMap map, int rank, String name) { - - // TODO: Implement this method - + map.put(rank, name); } /* @@ -43,10 +40,7 @@ Return the player with the highest ranking (smallest key). Output: "Jordan" */ public static String getTopPlayer(TreeMap map) { - - // TODO: Implement this method - - return null; + return map.firstEntry().getValue(); } /* @@ -58,10 +52,7 @@ Return the player with the lowest ranking (largest key). Output: "Taylor" */ public static String getLowestPlayer(TreeMap map) { - - // TODO: Implement this method - - return null; + return map.lastEntry().getValue(); } /* @@ -73,9 +64,7 @@ public static String getLowestPlayer(TreeMap map) { Output: player removed */ public static void removePlayer(TreeMap map, int rank) { - - // TODO: Implement this method - + map.remove(rank); } /* @@ -87,9 +76,6 @@ public static void removePlayer(TreeMap map, int rank) { Output: 3 */ public static Integer getNextRank(TreeMap map, int rank) { - - // TODO: Implement this method - - return null; + return rank + 1; } } diff --git a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java index dac03cf..c4d31e3 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -4,7 +4,6 @@ public class ArrayDequeProblems { public static void main(String[] args) { - ArrayDeque numbers = new ArrayDeque<>(); addToFront(numbers, 10); @@ -29,9 +28,7 @@ public static void main(String[] args) { Output: [5] */ public static void addToFront(ArrayDeque deque, int value) { - - // TODO: Implement this method - + deque.addFirst(value); } /* @@ -43,8 +40,7 @@ public static void addToFront(ArrayDeque deque, int value) { Output: [5,10] */ public static void addToBack(ArrayDeque deque, int value) { - - // TODO: Implement this method + deque.addLast(value); } @@ -57,8 +53,7 @@ public static void addToBack(ArrayDeque deque, int value) { Output: [10,20] */ public static void removeFront(ArrayDeque deque) { - - // TODO: Implement this method + deque.removeFirst(); } @@ -71,9 +66,7 @@ public static void removeFront(ArrayDeque deque) { Output: [5,10] */ public static void removeBack(ArrayDeque deque) { - - // TODO: Implement this method - + deque.removeLast(); } /* @@ -85,10 +78,7 @@ public static void removeBack(ArrayDeque deque) { Output: 5 */ public static Integer peekFront(ArrayDeque deque) { - - // TODO: Implement this method - - return null; + return deque.peekFirst(); } /* @@ -100,9 +90,6 @@ public static Integer peekFront(ArrayDeque deque) { Output: 20 */ 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..31218c1 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -1,12 +1,27 @@ 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 + ArrayDeque numbers = new ArrayDeque<>(); + + addFront(numbers, 10); + addFront(numbers, 5); + addBack(numbers, 20); + + System.out.println(peekFront(numbers)); + System.out.println(peekBack(numbers)); + + System.out.println("Deque: " + numbers); + + removeFront(numbers); + System.out.println("After removing front: " + numbers); + removeBack(numbers); + System.out.println("After removing back: " + numbers); } /* @@ -18,9 +33,7 @@ public static void main(String[] args) { Output: [5] */ public static void addFront(Deque deque, int value) { - - // TODO: Implement this method - + deque.addFirst(value); } /* @@ -32,9 +45,7 @@ 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 +57,7 @@ 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 +69,7 @@ public static Integer removeFront(Deque deque) { Output: 15 */ public static Integer removeBack(Deque deque) { - - // TODO: Implement this method - - return null; + return deque.removeLast(); } /* @@ -76,10 +81,7 @@ public static Integer removeBack(Deque deque) { Output: 5 */ public static Integer peekFront(Deque deque) { - - // TODO: Implement this method - - return null; + return deque.peekFirst(); } /* @@ -91,10 +93,7 @@ public static Integer peekFront(Deque deque) { Output: 15 */ 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..7c046ec 100644 --- a/src/main/java/Sets/HashSet/HashSetProblems.java +++ b/src/main/java/Sets/HashSet/HashSetProblems.java @@ -1,13 +1,20 @@ package Sets.HashSet; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Set; public class HashSetProblems { public static void main(String[] args) { - // You can test your methods here - + Set test = new HashSet<>(); + addElement(test, "apple"); + System.out.println(test); + System.out.println(containsValue(test, "apple")); + removeValue(test, "apple"); + System.out.println(test); + System.out.println(getUniqueValues(new ArrayList<>(List.of(1, 2, 2, 3, 3)))); } /* @@ -19,9 +26,7 @@ public static void main(String[] args) { Output: {"apple"} */ public static void addElement(Set set, String value) { - - // TODO: Implement this method - + set.add(value); } /* @@ -33,10 +38,7 @@ public static void addElement(Set set, String value) { Output: true or false */ public static boolean containsValue(Set set, String value) { - - // TODO: Implement this method - - return false; + return set.contains(value); } /* @@ -48,9 +50,7 @@ public static boolean containsValue(Set set, String value) { Output: value removed */ public static void removeValue(Set set, String value) { - - // TODO: Implement this method - + set.remove(value); } /* @@ -62,10 +62,7 @@ public static void removeValue(Set set, String value) { Output: 2 */ public static int getUniqueCount(Set set) { - - // TODO: Implement this method - - return 0; + return set.size(); } /* @@ -77,9 +74,6 @@ public static int getUniqueCount(Set set) { Output: {1,2,3} */ public static Set getUniqueValues(List numbers) { - - // TODO: Implement this method - - return null; + return new HashSet<>(numbers); } }