diff --git a/src/main/java/Collections/Practice/CollectionBasics.java b/src/main/java/Collections/Practice/CollectionBasics.java index e45cb49..1d3847d 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) { @@ -34,6 +35,15 @@ public static int sum(Collection numbers) { // Loop through the collection // Add each number to total + + //loop through each item in numbers + for(int item :numbers){ + + //add each item in loop to the total + total = total + item; + } + + //return final total value return total; } @@ -50,6 +60,15 @@ public static int countEven(Collection numbers) { // Loop through the collection // If the number is even, increase count + //loop through each item in numbers + for(int item : numbers){ + + //if item is divisible by 2 (remainder of item / 2 is equal to 0) + //then increment count by 1. + if(item % 2 ==0){ + count++; + } + } return count; } @@ -65,8 +84,16 @@ public static int findMax(Collection numbers) { // TODO: // Loop through numbers // Update max if current number is larger + for(int item : numbers){ + //if item is greater than max + if(item > max){ + //set max to item + max = item; + } + } + - return max; + return max; } @@ -81,7 +108,20 @@ public static boolean hasDuplicates(Collection numbers) { // Hint: // Compare the size of a collection with the size of a Set - return false; + //make new set, populate set with values in numbers. + //so this set will be a numbers list but without duplicates. + + HashSet set = new HashSet<>(); + + //add to set for each item in numbers + //but there is something that a + // set returns when you add something to a set, and its already in the set. + for(int item : numbers){ + + + } + + return false; } diff --git a/src/main/java/Hierachy/PRACTICE.md b/src/main/java/Hierachy/PRACTICE.md index 2b885d9..d8917ad 100644 --- a/src/main/java/Hierachy/PRACTICE.md +++ b/src/main/java/Hierachy/PRACTICE.md @@ -16,7 +16,7 @@ B. Iterable C. List D. Queue ---- +Answer :B. ## Question 2 @@ -27,7 +27,7 @@ B. Collection C. Map D. Deque ---- +Answer :B. ## Question 3 @@ -38,7 +38,7 @@ B. Set C. Queue D. Map ---- +Answer :D. ## Question 4 @@ -49,7 +49,8 @@ B. List C. Queue D. TreeSet ---- +Answer :B. + ## Question 5 @@ -60,17 +61,18 @@ B. Set C. Queue D. Map ---- +Answer :D. + # True or False Write **True** or **False**. -1. `ArrayList` implements the `List` interface. -2. `Set` allows duplicate elements. -3. `Map` stores elements using keys and values. -4. `Queue` typically follows FIFO behavior. -5. `Iterable` allows collections to be used in enhanced for-loops. +1. `ArrayList` implements the `List` interface. Answer : True +2. `Set` allows duplicate elements. Answer : False +3. `Map` stores elements using keys and values. Answer : True +4. `Queue` typically follows FIFO behavior. Answer : True +5. `Iterable` allows collections to be used in enhanced for-loops. Answer : True --- @@ -80,13 +82,13 @@ Write **True** or **False**. What is the difference between an **interface** and a **class** in the Java Collections Framework? ---- +Answer: Interface defines behaviors and class implements those behaviors ## Question 2 Why is it recommended to declare variables using interfaces like `List` instead of concrete classes like `ArrayList`? -Example: +Answer: It provides better flexibilities, abstraction and better code design. ```java List list = new ArrayList<>(); @@ -97,8 +99,9 @@ List list = new ArrayList<>(); ## Question 3 Name three interfaces that extend `Collection`. +Answer: Lists, Set, and Queue. + ---- # Code Reading Exercise @@ -117,10 +120,9 @@ for (String fruit : fruits) { Questions: -1. Which interface type is used for the variable? -2. Which class is used as the implementation? -3. Why can the enhanced for-loop be used here? - +1. Which interface type is used for the variable? Answer: List +2. Which class is used as the implementation? Answer: ArrayList +3. Why can the enhanced for-loop be used here? Answer:Lists extends Collections which extends iterable and the enhanced for loop can be used for on all collections. --- # Coding Exercise diff --git a/src/main/java/Iterable/Examples/ForEachLoopDemo.java b/src/main/java/Iterable/Examples/ForEachLoopDemo.java index a6d2198..57723f5 100644 --- a/src/main/java/Iterable/Examples/ForEachLoopDemo.java +++ b/src/main/java/Iterable/Examples/ForEachLoopDemo.java @@ -5,7 +5,6 @@ public class ForEachLoopDemo { public static void main(String[] args) { - List students = new ArrayList<>(); students.add("Alex"); @@ -15,23 +14,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 - +// Loop through each student and print their 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 - +// Loop through each student and print 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 +// Loop through students and increment count + 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..4df5e2d 100644 --- a/src/main/java/Iterable/Examples/IteratorDemo.java +++ b/src/main/java/Iterable/Examples/IteratorDemo.java @@ -6,7 +6,6 @@ public class IteratorDemo { public static void main(String[] args) { - List numbers = new ArrayList<>(); numbers.add(10); @@ -18,26 +17,33 @@ public static void main(String[] args) { System.out.println("Original list:"); System.out.println(numbers); - // Create iterator +// Create iterator Iterator iterator = numbers.iterator(); System.out.println("\nIterating using Iterator:"); - // TODO: - // Use iterator.hasNext() and iterator.next() - // Print each number +// Loop through list using iterator + while (iterator.hasNext()) { + int number = iterator.next(); // get next number + System.out.println(number); // print it + } 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()) { + + int number = iterator.next(); + if (number % 2 != 0) { // check if number is odd + iterator.remove(); // remove it safely + } + } 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..fd45493 100644 --- a/src/main/java/Iterable/Practice/IterableWarmups.java +++ b/src/main/java/Iterable/Practice/IterableWarmups.java @@ -4,32 +4,18 @@ import java.util.List; public class IterableWarmups { - public static void main(String[] args) { - - List numbers = new ArrayList<>(); - - numbers.add(3); - numbers.add(7); - numbers.add(10); - numbers.add(4); - numbers.add(8); - - System.out.println("Sum: " + sum(numbers)); - System.out.println("Even count: " + countEven(numbers)); - System.out.println("Max value: " + findMax(numbers)); - } - - /* - PROBLEM 1 - Return the sum of all numbers in the iterable - */ + 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 + // Loop through each number in the iterable + for (int num : numbers) { + total += num; // add each number to total + } return total; } @@ -38,14 +24,19 @@ public static int sum(Iterable numbers) { /* 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 + // Loop through each number + for (int num : numbers) { + + // Check if the number is even + if (num % 2 == 0) { + count++; // increase counter + } + } return count; } @@ -54,14 +45,19 @@ public static int countEven(Iterable numbers) { /* 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 (int num : numbers) { + + // If current number is greater than max + if (num > max) { + max = num; // update max + } + } return max; } @@ -70,15 +66,20 @@ public static int findMax(Iterable numbers) { /* 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 + // Loop through each word + for (String word : words) { + + // Compare word to target + 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..b0a2fa6 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -5,101 +5,102 @@ import java.util.List; public class ArrayListProblems { - public static void main(String[] args) { - - List numbers = new ArrayList<>(); - - numbers.add(4); - numbers.add(7); - numbers.add(2); - numbers.add(7); - numbers.add(9); - numbers.add(4); - - System.out.println("Sum: " + sum(numbers)); - System.out.println("Even Count: " + countEvens(numbers)); - System.out.println("Contains Duplicate: " + hasDuplicate(numbers)); - System.out.println("Max Value: " + findMax(numbers)); - System.out.println("Reversed List: " + reverse(numbers)); - } - /* - 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 + public class ListPractice { - return 0; - } + public static void main(String[] args) { - /* - Problem 2 - Count how many EVEN numbers exist in the list. + List numbers = new ArrayList<>(); - Example - Input: [1,2,4,7] - Output: 2 - */ - public static int countEvens(List nums) { + numbers.add(4); + numbers.add(7); + numbers.add(2); + numbers.add(7); + numbers.add(9); + numbers.add(4); - // TODO: Implement this method + System.out.println("Sum: " + sum(numbers)); + System.out.println("Even Count: " + countEvens(numbers)); + System.out.println("Contains Duplicate: " + hasDuplicate(numbers)); + System.out.println("Max Value: " + findMax(numbers)); + System.out.println("Reversed List: " + reverse(numbers)); + } - return 0; - } - /* - Problem 3 - Determine if the list contains any duplicate values. + // Problem 1 + public static int sum(List nums) { - Example - Input: [1,2,3,1] - Output: true + int total = 0; - Input: [1,2,3] - Output: false - */ - public static boolean hasDuplicate(List nums) { + for (int num : nums) { + total += num; + } - // TODO: Implement this method + return total; + } - return false; - } - /* - Problem 4 - Return the largest number in the list. + // Problem 2 + public static int countEvens(List nums) { - Example - Input: [4,2,7] - Output: 7 - */ - public static int findMax(List nums) { + int count = 0; - // TODO: Implement this method + for (int num : nums) { - return 0; - } + if (num % 2 == 0) { + count++; + } + } + + return count; + } + + + // Problem 3 + public static boolean hasDuplicate(List nums) { + + 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 + public static int findMax(List nums) { + + 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] + // Problem 5 + public static List reverse(List nums) { - The original list should remain unchanged. - */ - public static List reverse(List nums) { + List reversed = new ArrayList<>(); - // TODO: Implement this method + for (int i = nums.size() - 1; i >= 0; i--) { + reversed.add(nums.get(i)); + } - return null; + 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..176194e 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -3,113 +3,73 @@ import java.util.LinkedList; public class LinkedListProblems { - public static void main(String[] args) { - LinkedList numbers = new LinkedList<>(); + public class LinkedListPractice { - numbers.add(10); - numbers.add(20); - numbers.add(30); - numbers.add(40); - numbers.add(50); + public static void main(String[] args) { - addToFront(numbers, 5); - addToEnd(numbers, 60); + LinkedList numbers = new LinkedList<>(); - System.out.println("List after additions: " + numbers); + numbers.add(10); + numbers.add(20); + numbers.add(30); + numbers.add(40); + numbers.add(50); - removeFirstElement(numbers); - removeLastElement(numbers); + addToFront(numbers, 5); + addToEnd(numbers, 60); - System.out.println("List after removals: " + numbers); + System.out.println("List after additions: " + numbers); - System.out.println("First Element: " + getFirstElement(numbers)); - System.out.println("Last Element: " + getLastElement(numbers)); - } - - /* - 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 - - } + removeFirstElement(numbers); + removeLastElement(numbers); - /* - Problem 2 - Add a value to the END of the LinkedList. + System.out.println("List after removals: " + numbers); - Example - Input: [10,20,30], value=40 - Output: [10,20,30,40] - */ - public static void addToEnd(LinkedList list, int value) { + System.out.println("First Element: " + getFirstElement(numbers)); + System.out.println("Last Element: " + getLastElement(numbers)); + } - // TODO: Implement this method + // Problem 1 + public static void addToFront(LinkedList list, int value) { - } + list.addFirst(value); - /* - Problem 3 - Remove the FIRST element from the LinkedList. + } - Example - Input: [10,20,30] - Output: [20,30] - */ - public static void removeFirstElement(LinkedList list) { + // Problem 2 + public static void addToEnd(LinkedList list, int value) { - // TODO: Implement this method + list.addLast(value); - } + } - /* - Problem 4 - Remove the LAST element from the LinkedList. + // Problem 3 + public static void removeFirstElement(LinkedList list) { - Example - Input: [10,20,30] - Output: [10,20] - */ - public static void removeLastElement(LinkedList list) { + list.removeFirst(); - // TODO: Implement this method + } - } + // Problem 4 + public static void removeLastElement(LinkedList list) { - /* - Problem 5 - Return the FIRST element in the LinkedList. + list.removeLast(); - Example - Input: [10,20,30] - Output: 10 - */ - public static int getFirstElement(LinkedList list) { + } - // TODO: Implement this method + // Problem 5 + public static int getFirstElement(LinkedList list) { - return 0; - } + return list.getFirst(); - /* - Problem 6 - Return the LAST element in the LinkedList. + } - Example - Input: [10,20,30] - Output: 30 - */ - public static int getLastElement(LinkedList list) { + // Problem 6 + public static int getLastElement(LinkedList list) { - // TODO: Implement this method + return list.getLast(); - return 0; + } } -} +} \ 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..4e4b734 100644 --- a/src/main/java/Maps/HashMap/HashMapProblems.java +++ b/src/main/java/Maps/HashMap/HashMapProblems.java @@ -27,72 +27,70 @@ public static void main(String[] args) { /* 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 + if (map.containsKey(item)) { + return map.get(item); + } return 0; } + /* 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 + Map frequency = new HashMap<>(); + + for (int num : numbers) { + + if (frequency.containsKey(num)) { + frequency.put(num, frequency.get(num) + 1); + } else { + frequency.put(num, 1); + } + + } - return null; + return frequency; } -} +} \ 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..e7f3501 100644 --- a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java +++ b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java @@ -31,23 +31,30 @@ public static void main(String[] args) { Output: {"Jordan"=90} */ public static void addStudent(Map map, String name, int grade) { + Map studentGrades = new LinkedHashMap<>(); - // TODO: Implement this method + // add the student name as the key and grade as the value + map.put(name, grade); + + } - } /* - Problem 2 - Update the grade of an existing student. + Problem 2 + Update the grade of an existing student. - Example - Input: ("Taylor", 92) - Output: {"Taylor"=92} - */ + Example + Input: ("Taylor", 92) + Output: {"Taylor"=92} + */ public static void updateGrade(Map map, String name, int newGrade) { - // TODO: Implement this method + // check if the student exists in the map + if (map.containsKey(name)) { + // update the grade + map.put(name, newGrade); + } } /* @@ -60,8 +67,14 @@ public static void updateGrade(Map map, String name, int newGra */ public static void removeStudent(Map map, String name) { - // TODO: Implement this method + // check if student exists + if (map.containsKey(name)) { + + // remove the student + map.remove(name); + System.out.println("Student removed from map"); + } } /* @@ -74,8 +87,14 @@ public static void removeStudent(Map map, String name) { */ public static String getFirstInserted(Map map) { - // TODO: Implement this method + // loop through the keys + for (String name : map.keySet()) { + // return the first key encountered + return name; + } + + // if map is empty return null; } @@ -90,8 +109,22 @@ public static String getFirstInserted(Map map) { */ public static Map wordFrequency(List words) { - // TODO: Implement this method + Map frequencyMap = new LinkedHashMap<>(); - return null; + // loop through the list + for (String word : words) { + + // if word already exists increase count + if (frequencyMap.containsKey(word)) { + frequencyMap.put(word, frequencyMap.get(word) + 1); + } + + // if word appears first time + else { + frequencyMap.put(word, 1); + } + } + + return frequencyMap; } -} +} \ 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..582ced5 100644 --- a/src/main/java/Maps/TreeMap/TreeMapProblems.java +++ b/src/main/java/Maps/TreeMap/TreeMapProblems.java @@ -5,91 +5,57 @@ public class TreeMapProblems { public static void main(String[] args) { - TreeMap rankings = new TreeMap<>(); - addPlayer(rankings, 3, "Jordan"); - addPlayer(rankings, 1, "Taylor"); - addPlayer(rankings, 2, "Morgan"); - - System.out.println("Rankings: " + rankings); - - System.out.println("Top Player: " + getTopPlayer(rankings)); - System.out.println("Lowest Ranked Player: " + getLowestPlayer(rankings)); - - removePlayer(rankings, 2); - System.out.println("After removal: " + rankings); } - /* - Problem 1 - Add a player to the TreeMap with their rank. - - Example - Input: (1, "Jordan") - Output: {1="Jordan"} - */ +Problem 1 +Add a player to the TreeMap with their rank. +*/ public static void addPlayer(TreeMap map, int rank, String name) { - // TODO: Implement this method - + // add rank as key and player name as value + 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; + // get value of the first (smallest) key + return map.get(map.firstKey()); } /* 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; + // get value of the last (largest) key + return map.get(map.lastKey()); } /* 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 + // remove player with that rank + map.remove(rank); + System.out.println("player removed"); } /* 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; + // returns the next higher key + return map.higherKey(rank); } -} +} \ 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..325ffe4 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -21,88 +21,62 @@ public static void main(String[] args) { } /* - Problem 1 - Add a number to the FRONT of the deque. - - Example - Input: value = 5 - Output: [5] - */ + Problem 1 + Add a number to the FRONT of the deque. + */ public static void addToFront(ArrayDeque deque, int value) { - // TODO: Implement this method - + // adds the value to the front of the deque + deque.addFirst(value); } /* Problem 2 Add a number to the BACK of the deque. - - Example - Input: value = 10 - Output: [5,10] */ public static void addToBack(ArrayDeque deque, int value) { - // TODO: Implement this method - + // adds the value to the back of the deque + deque.addLast(value); } /* Problem 3 Remove the element at the FRONT of the deque. - - Example - Input: [5,10,20] - Output: [10,20] */ public static void removeFront(ArrayDeque deque) { - // TODO: Implement this method - + // removes the first element + deque.removeFirst(); } /* Problem 4 Remove the element at the BACK of the deque. - - Example - Input: [5,10,20] - Output: [5,10] */ public static void removeBack(ArrayDeque deque) { - // TODO: Implement this method - + // removes the last element + deque.removeLast(); } /* Problem 5 Return the FIRST element in the deque without removing it. - - Example - Input: [5,10,20] - Output: 5 */ public static Integer peekFront(ArrayDeque deque) { - // TODO: Implement this method - - return null; + // returns first element without removing it + return deque.peekFirst(); } /* Problem 6 Return the LAST element in the deque without removing it. - - Example - Input: [5,10,20] - Output: 20 */ public static Integer peekBack(ArrayDeque deque) { - // TODO: Implement this method - - return null; + // returns last element without removing it + return deque.peekLast(); } -} +} \ No newline at end of file diff --git a/src/main/java/Queues/Deque/DequeProblems.java b/src/main/java/Queues/Deque/DequeProblems.java index 7ef0c06..778985e 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -12,89 +12,60 @@ public static void main(String[] args) { /* Problem 1 Add a value to the FRONT of the deque. - - Example - Input: value = 5 - Output: [5] */ public static void addFront(Deque deque, int value) { - // TODO: Implement this method - + // add value to the front + deque.addFirst(value); } /* Problem 2 Add a value to the BACK of the deque. - - Example - Input: value = 10 - Output: [5,10] */ public static void addBack(Deque deque, int value) { - // TODO: Implement this method - + // add value to the back + deque.addLast(value); } /* Problem 3 Remove and return the FRONT element of the deque. - - Example - Input: [5,10,15] - Output: 5 */ public static Integer removeFront(Deque deque) { - // TODO: Implement this method - - return null; + // remove and return first element + return deque.removeFirst(); } /* Problem 4 Remove and return the BACK element of the deque. - - Example - Input: [5,10,15] - Output: 15 */ public static Integer removeBack(Deque deque) { - // TODO: Implement this method - - return null; + // remove and return last element + return deque.removeLast(); } /* Problem 5 Return the FRONT element without removing it. - - Example - Input: [5,10,15] - Output: 5 */ public static Integer peekFront(Deque deque) { - // TODO: Implement this method - - return null; + // look at first element without removing + return deque.peekFirst(); } /* Problem 6 Return the BACK element without removing it. - - Example - Input: [5,10,15] - Output: 15 */ public static Integer peekBack(Deque deque) { - // TODO: Implement this method - - return null; + // look at last element without removing + return deque.peekLast(); } - -} +} \ No newline at end of file diff --git a/src/main/java/Sets/HashSet/HashSetProblems.java b/src/main/java/Sets/HashSet/HashSetProblems.java index a36c570..49fbb7e 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; @@ -13,73 +14,54 @@ 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 - + // add the value to the set + 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 false; + // return true if set contains the value + return set.contains(value); } /* 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 + // remove the value from the set + set.remove(value); + System.out.println("value removed"); } /* 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 0; + // size() returns number of unique values + return set.size(); } /* 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 + // HashSet automatically removes duplicates + Set uniqueValues = new HashSet<>(numbers); - return null; + return uniqueValues; } -} +} \ No newline at end of file