diff --git a/src/main/java/Collections/Practice/CollectionBasics.java b/src/main/java/Collections/Practice/CollectionBasics.java index e45cb49..32975a3 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) { @@ -27,9 +28,11 @@ public static void main(String[] args) { Return the sum of all numbers in the collection */ public static int sum(Collection numbers) { - int total = 0; + for (int num : numbers) { + total += num; + } // TODO: // Loop through the collection // Add each number to total @@ -43,9 +46,13 @@ public static int sum(Collection numbers) { Count how many numbers are even */ public static int countEven(Collection numbers) { - int count = 0; + for (int num : numbers) { + if (num % 2 == 0) { + count++; + } + } // TODO: // Loop through the collection // If the number is even, increase count @@ -59,9 +66,13 @@ public static int countEven(Collection numbers) { Find the largest number in the collection */ public static int findMax(Collection numbers) { - int max = Integer.MIN_VALUE; + for (int num : numbers) { + if (num > max) { + max = num; + } + } // TODO: // Loop through numbers // Update max if current number is larger @@ -90,9 +101,14 @@ public static boolean hasDuplicates(Collection numbers) { Count how many times a target value appears */ public static int countOccurrences(Collection numbers, int target) { - int count = 0; + for (int num : numbers) { + if (num == target) { + count++; + } + } + // TODO: // Loop through numbers // If number equals target, increase count @@ -107,9 +123,14 @@ public static int countOccurrences(Collection numbers, int target) { that only contains numbers greater than 20 */ public static Collection filterGreaterThanTwenty(Collection numbers) { - Collection result = new ArrayList<>(); + for (int num : numbers) { + if (num > 20) { + result.add(num); + } + } + // TODO: // Loop through numbers // Add numbers greater than 20 to result diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java index 6c2f4e1..cf8939a 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -1,11 +1,10 @@ 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) { @@ -21,11 +20,17 @@ public static void main(String[] args) { Input: [1,2,2,3,4,4,5] Output: [1,2,3,4,5] */ - public static List removeDuplicates(List numbers) { + + + public static List removeDuplicates(List numbers) { + Set set = new LinkedHashSet<>(numbers);//Have to convert List to Set + //Set removes duplicates *LinkedHashSet Keeps the value in order + return new ArrayList<>(set);//Covert the Set back to List + // because the method calls for a List *Set is just for removing Duplicates // TODO: Implement this method - return null; + } /* @@ -37,10 +42,20 @@ public static List removeDuplicates(List numbers) { Output: {1=1, 2=2, 3=3} */ public static Map countFrequency(List numbers) { - + //takes a list of numbers + //returns a map of counts + Map map = new HashMap<>();//This creates an empty HashMap called map + for (int num : numbers) { //for each loop to run through the list + map.put(num, map.getOrDefault(num, 0) + 1); + //This line basically updates the counter for the number + //it Gets the value for this key + // and If the key does not exist, return 0 + } + + return map;//returns the final results // TODO: Implement this method - return null; + } /* @@ -52,10 +67,21 @@ public static Map countFrequency(List numbers) { Output: 5 */ public static Integer firstUnique(List numbers) { + //THe method header + Map map = countFrequency(numbers);//calling - // TODO: Implement this method + for (int num : numbers) { + if (map.get(num) == 1) { + //get the count for this number + return num; + //returning the number that appears once + } + } return null; + // TODO: Implement this method + + } /* @@ -69,10 +95,21 @@ public static Integer firstUnique(List numbers) { Output: true */ public static boolean twoSum(List numbers, int target) { + Set seen = new HashSet<>(); + //This set stores numbers we have already seen. - // TODO: Implement this method + for (int num : numbers) { + if (seen.contains(target - num)) { + //if we already saw the number that would complete the target. + return true; + } + seen.add(num);//adds the number into the seen object that was created + } return false; + // TODO: Implement this method + + } /* @@ -84,10 +121,11 @@ public static boolean twoSum(List numbers, int target) { Output: 3 */ public static int countUniqueWords(List words) { - + Set set = new HashSet<>(words); + return set.size(); // TODO: Implement this method - return 0; + } /* @@ -100,9 +138,27 @@ public static int countUniqueWords(List words) { */ public static Queue reverseQueue(Queue queue) { + // Create a stack to help reverse the order + // Stacks are LIFO (Last In First Out) + Stack stack = new Stack<>(); + + // Move all elements from the queue into the stack + // Queue removes from the front using poll() + while (!queue.isEmpty()) { + stack.push(queue.poll()); // remove from queue and push into stack + } + + // Now move everything back from the stack into the queue + // Since stacks remove the last element added, the order becomes reversed + while (!stack.isEmpty()) { + queue.offer(stack.pop()); // pop from stack and add back to queue + } + + // Return the reversed queue + return queue; // TODO: Implement this method - return null; + } /* @@ -118,9 +174,37 @@ public static Queue reverseQueue(Queue queue) { */ public static boolean isBalanced(String expression) { + // Create a stack to keep track of opening parentheses + Stack stack = new Stack<>(); + + // Loop through each character in the string + for (char c : expression.toCharArray()) { + + // If we see an opening parenthesis, push it onto the stack + if (c == '(') { + stack.push(c); + } + + // If we see a closing parenthesis + else if (c == ')') { + + // If the stack is empty, there was no matching '(' + // Example: ")(" + if (stack.isEmpty()) { + return false; + } + + // Otherwise remove the matching '(' from the stack + stack.pop(); + } + } + + // If the stack is empty, all parentheses matched correctly + // If not empty, there are extra '(' left + return stack.isEmpty(); // TODO: Implement this method - return false; + } /* @@ -133,9 +217,35 @@ public static boolean isBalanced(String expression) { */ public static Integer mostFrequent(List numbers) { + // Count how many times each number appears + + Map map = countFrequency(numbers); + + // Track the highest frequency seen so far + int max = 0; + + // Track which number has that highest frequency + Integer result = null; + + // Loop through each number in the map + for (int key : map.keySet()) { + + // If this number occurs more times than the current max + if (map.get(key) > max) { + + // Update max frequency + max = map.get(key); + + // Update the number that has the highest frequency + result = key; + } + } + + // Return the number that appears most frequently + return result; // TODO: Implement this method - return null; + } /* @@ -153,9 +263,32 @@ public static Integer mostFrequent(List numbers) { */ public static Map> groupByLength(List words) { + // Create a map where: + // key = length of a word + // value = list of words with that length + Map> map = new HashMap<>(); + + // Loop through each word in the input list + for (String word : words) { + + // Find the length of the current word + int length = word.length(); + + // If this length is not already a key in the map + // create a new list for this length + if (!map.containsKey(length)) { + map.put(length, new ArrayList<>()); + } + + // Add the word to the list for its length + map.get(length).add(word); + } + + // Return the map grouping words by their length + return map; // TODO: Implement this method - return null; + } /* @@ -170,9 +303,31 @@ public static Map> groupByLength(List words) { */ public static int maxSlidingWindowSum(List numbers, int k) { + // Initialize a variable to keep track of the maximum sum + int maxSum = 0; + + // Loops over each possible starting index of a window of size k + for (int i = 0; i <= numbers.size() - k; i++) { + + // Initialize sum for the current window + int currentSum = 0; + + //Sum up all numbers in the current window + for (int j = i; j < i + k; j++) { + currentSum += numbers.get(j); + } + + // Update maxSum if the current window sum is bigger + if (currentSum > maxSum) { + maxSum = currentSum; + } + } + + // Returns the largest sum found among all windows + return maxSum; // TODO: Implement this method - return 0; + } } -} + diff --git a/src/main/java/Hierachy/01-README.md b/src/main/java/Hierachy/01-README.md index d84a981..c4a4243 100644 --- a/src/main/java/Hierachy/01-README.md +++ b/src/main/java/Hierachy/01-README.md @@ -21,16 +21,17 @@ Classes implement those behaviors. ``` -Iterable -↓ -Collection -↓ -List Set Queue -↓ ↓ ↓ -ArrayList HashSet PriorityQueue -LinkedList LinkedHashSet -Vector TreeSet -Stack + + Iterable + ↓ + Collection + ↓ + List Set Queue + ↓ ↓ ↓ + ArrayList HashSet PriorityQueue + LinkedList LinkedHashSet + Vector TreeSet + Stack ``` @@ -38,8 +39,8 @@ Important note: ``` -Map -↓ + Map + ↓ HashMap LinkedHashMap TreeMap diff --git a/src/main/java/Iterable/Examples/ForEachLoopDemo.java b/src/main/java/Iterable/Examples/ForEachLoopDemo.java index a6d2198..41e7654 100644 --- a/src/main/java/Iterable/Examples/ForEachLoopDemo.java +++ b/src/main/java/Iterable/Examples/ForEachLoopDemo.java @@ -16,12 +16,21 @@ public static void main(String[] args) { System.out.println("Printing students using a for-each loop:"); // TODO: + for (String student: students) { + System.out.println(student); + + } + // Use a for-each loop to print each student name System.out.println("\nPrinting students in uppercase:"); // TODO: + for (String student: students){ + System.out.println(student.toUpperCase()); + } + // Use a for-each loop to print each name in uppercase @@ -29,7 +38,10 @@ public static void main(String[] args) { int count = 0; - // TODO: + // TODO: increment the count variable inside the loop. + for (String student : students){ + count++; + } // Use a for-each loop to count how many students are in the list 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..42fdf24 100644 --- a/src/main/java/Iterable/Examples/IteratorDemo.java +++ b/src/main/java/Iterable/Examples/IteratorDemo.java @@ -24,6 +24,12 @@ public static void main(String[] args) { System.out.println("\nIterating using Iterator:"); // TODO: + //hasNext() → Is there another item? + //next() → Move to it and give it to me + while (iterator.hasNext()) { + Integer num = iterator.next(); + System.out.println(num); + } // Use iterator.hasNext() and iterator.next() // Print each number @@ -33,6 +39,12 @@ public static void main(String[] args) { iterator = numbers.iterator(); // TODO: + while (iterator.hasNext()) { + Integer num = iterator.next();//Stored the next int in list in "num" + if (num % 2 !=0){ //If numbers divided by 2 not equal to 0 + iterator.remove();//Then remove it + } + } // Use iterator to remove odd numbers // Remember: use iterator.remove() diff --git a/src/main/java/Iterable/Practice/IterableWarmups.java b/src/main/java/Iterable/Practice/IterableWarmups.java index 9e9de94..16c29a9 100644 --- a/src/main/java/Iterable/Practice/IterableWarmups.java +++ b/src/main/java/Iterable/Practice/IterableWarmups.java @@ -17,6 +17,7 @@ public static void main(String[] args) { System.out.println("Sum: " + sum(numbers)); System.out.println("Even count: " + countEven(numbers)); System.out.println("Max value: " + findMax(numbers)); + // System.out.println("Count matches: " + countMatches()); } @@ -27,8 +28,10 @@ public static void main(String[] args) { public static int sum(Iterable numbers) { int total = 0; - // TODO: + for (Integer sum : numbers) { + total += sum;// goes through each number and adds it to the next for total + } // Use a for-each loop to calculate the sum return total; @@ -44,6 +47,11 @@ public static int countEven(Iterable numbers) { int count = 0; // TODO: + for (Integer num : numbers) { + if (num %2 == 0) { + count++; + } + } // Loop through numbers // Increment count if number is even @@ -60,6 +68,10 @@ public static int findMax(Iterable numbers) { int max = Integer.MIN_VALUE; // TODO: + for (Integer num : numbers){ + if(num > max) + max = num; + } // Loop through numbers // Update max if current number is larger @@ -76,6 +88,12 @@ public static int countMatches(Iterable words, String target) { int count = 0; // TODO: + for (String word : words){ + if (words.equals(target)) { + count++; + } + + } // Loop through words // Compare each word to target diff --git a/src/main/java/Lists/ArrayLists/ArrayListProblems.java b/src/main/java/Lists/ArrayLists/ArrayListProblems.java index baf4dfa..d053e53 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -34,8 +34,12 @@ public static void main(String[] args) { public static int sum(List nums) { // TODO: Implement this method + int sum = 0; + for (int n : nums) { + sum += n; + } + return sum; - return 0; } /* @@ -49,8 +53,13 @@ public static int sum(List nums) { public static int countEvens(List nums) { // TODO: Implement this method + int count = 0; + for (int n : nums) { + if (n % 2 ==0) count++; - return 0; + } + + return count; } /* @@ -67,6 +76,11 @@ public static int countEvens(List nums) { public static boolean hasDuplicate(List nums) { // TODO: Implement this method + List Dupes = new ArrayList<>(); + for (int n : nums){ + if (Dupes.contains(n)) return true; + Dupes.add(n); + } return false; } @@ -82,8 +96,13 @@ public static boolean hasDuplicate(List nums) { public static int findMax(List nums) { // TODO: Implement this method - - return 0; + List Largest = new ArrayList<>(); + if (nums.isEmpty()) throw new IllegalArgumentException("List is Empty"); + int max = nums.get(0); + for (int n : nums){ + if (n > max) max = n; + } + return max; } /* @@ -99,7 +118,11 @@ public static int findMax(List nums) { public static List reverse(List nums) { // TODO: Implement this method + List Reversed = new ArrayList<>(); + for (int i = nums.size() - 1; i >= 0; i--){ + Reversed.add(nums.get(i)); + } - return null; + return Reversed; } } diff --git a/src/main/java/Lists/LinkedLists/LinkedListProblems.java b/src/main/java/Lists/LinkedLists/LinkedListProblems.java index 178a2ba..b9f8c24 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -36,7 +36,7 @@ public static void main(String[] args) { Output: [5,10,20,30] */ public static void addToFront(LinkedList list, int value) { - + list.add(0, value); // TODO: Implement this method } @@ -50,7 +50,7 @@ public static void addToFront(LinkedList list, int value) { Output: [10,20,30,40] */ public static void addToEnd(LinkedList list, int value) { - + list.add(value); // TODO: Implement this method } @@ -64,7 +64,9 @@ public static void addToEnd(LinkedList list, int value) { Output: [20,30] */ public static void removeFirstElement(LinkedList list) { - + if (!list.isEmpty()) { + list.remove(0); + } // TODO: Implement this method } @@ -78,7 +80,9 @@ public static void removeFirstElement(LinkedList list) { Output: [10,20] */ public static void removeLastElement(LinkedList list) { - + if (!list.isEmpty()) { + list.remove(list.size()-1); + } // TODO: Implement this method } @@ -94,8 +98,9 @@ public static void removeLastElement(LinkedList list) { public static int getFirstElement(LinkedList list) { // TODO: Implement this method - - return 0; + return list.get(0); + //return list.getFirst(); i tried this first and it worked also + //return 0; } /* @@ -107,9 +112,10 @@ public static int getFirstElement(LinkedList list) { Output: 30 */ public static int getLastElement(LinkedList list) { - + return list.get(list.size() - 1); + //return list.getLast(); i tried this first and it worked also // TODO: Implement this method - return 0; + //return 0; } } diff --git a/src/main/java/Maps/HashMap/HashMapProblems.java b/src/main/java/Maps/HashMap/HashMapProblems.java index 04b5567..9dd34fa 100644 --- a/src/main/java/Maps/HashMap/HashMapProblems.java +++ b/src/main/java/Maps/HashMap/HashMapProblems.java @@ -33,7 +33,7 @@ public static void main(String[] args) { Output: {"Apples"=10} */ public static void addItem(Map map, String item, int quantity) { - + map.put(item, quantity); // TODO: Implement this method } @@ -47,7 +47,9 @@ public static void addItem(Map map, String item, int quantity) Output: 10 */ public static int getQuantity(Map map, String item) { - + if (map.containsKey(item)) { + return map.get(item); + } // TODO: Implement this method return 0; @@ -62,7 +64,9 @@ public static int getQuantity(Map map, String item) { Output: {"Bananas"=12} */ public static void updateQuantity(Map map, String item, int newQuantity) { - + if (map.containsKey(item)) { + map.put(item, newQuantity); + } // TODO: Implement this method } @@ -76,7 +80,7 @@ public static void updateQuantity(Map map, String item, int new Output: item removed */ public static void removeItem(Map map, String item) { - + map.remove(item); // TODO: Implement this method } @@ -90,9 +94,20 @@ public static void removeItem(Map map, String item) { Output: {1=1, 2=2, 3=3} */ public static Map countFrequency(List numbers) { + Map frequency = new HashMap<>(); + for (int num : numbers) { + + if (frequency.containsKey(num)) { + frequency.put(num, frequency.get(num) + 1); + } else { + frequency.put(num, 1); + } + + } // TODO: Implement this method - return null; + return frequency; + } } diff --git a/src/main/java/Maps/TreeMap/TreeMapProblems.java b/src/main/java/Maps/TreeMap/TreeMapProblems.java index b101d2e..50c2bda 100644 --- a/src/main/java/Maps/TreeMap/TreeMapProblems.java +++ b/src/main/java/Maps/TreeMap/TreeMapProblems.java @@ -29,7 +29,7 @@ public static void main(String[] args) { Output: {1="Jordan"} */ public static void addPlayer(TreeMap map, int rank, String name) { - + map.put(rank, name); // TODO: Implement this method } @@ -43,10 +43,10 @@ Return the player with the highest ranking (smallest key). Output: "Jordan" */ public static String getTopPlayer(TreeMap map) { - + return map.get(map.firstKey()); // TODO: Implement this method - return null; + } /* @@ -58,10 +58,10 @@ Return the player with the lowest ranking (largest key). Output: "Taylor" */ public static String getLowestPlayer(TreeMap map) { - + return map.get(map.lastKey()); // TODO: Implement this method - return null; + } /* @@ -73,7 +73,7 @@ public static String getLowestPlayer(TreeMap map) { Output: player removed */ public static void removePlayer(TreeMap map, int rank) { - + map.remove(rank); // TODO: Implement this method } @@ -87,9 +87,9 @@ public static void removePlayer(TreeMap map, int rank) { Output: 3 */ public static Integer getNextRank(TreeMap map, int rank) { - + return map.higherKey(rank); // TODO: Implement this method - return null; + } } diff --git a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java index dac03cf..5684244 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -29,7 +29,7 @@ public static void main(String[] args) { Output: [5] */ public static void addToFront(ArrayDeque deque, int value) { - + deque.addFirst(value); // TODO: Implement this method } @@ -43,7 +43,7 @@ public static void addToFront(ArrayDeque deque, int value) { Output: [5,10] */ public static void addToBack(ArrayDeque deque, int value) { - + deque.addLast(value); // TODO: Implement this method } @@ -57,7 +57,7 @@ public static void addToBack(ArrayDeque deque, int value) { Output: [10,20] */ public static void removeFront(ArrayDeque deque) { - + deque.removeFirst(); // TODO: Implement this method } @@ -71,7 +71,7 @@ public static void removeFront(ArrayDeque deque) { Output: [5,10] */ public static void removeBack(ArrayDeque deque) { - + deque.removeLast(); // TODO: Implement this method } @@ -85,10 +85,9 @@ public static void removeBack(ArrayDeque deque) { Output: 5 */ public static Integer peekFront(ArrayDeque deque) { - + return deque.peekFirst(); // TODO: Implement this method - return null; } /* @@ -100,9 +99,8 @@ public static Integer peekFront(ArrayDeque deque) { Output: 20 */ public static Integer peekBack(ArrayDeque deque) { - + return deque.peekLast(); // TODO: Implement this method - return null; } } diff --git a/src/main/java/Queues/Deque/DequeProblems.java b/src/main/java/Queues/Deque/DequeProblems.java index 7ef0c06..1875da9 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -6,6 +6,9 @@ public class DequeProblems { public static void main(String[] args) { // You can test your methods here + //Your code doesn’t show values or an ArrayList because a + // Deque is just an interface. It describes behavior(front/back operations), + // but it is not the actual data structure storing the values. } @@ -18,7 +21,7 @@ public static void main(String[] args) { Output: [5] */ public static void addFront(Deque deque, int value) { - + deque.addFirst(value); // TODO: Implement this method } @@ -32,7 +35,7 @@ public static void addFront(Deque deque, int value) { Output: [5,10] */ public static void addBack(Deque deque, int value) { - + deque.addLast(value); // TODO: Implement this method } @@ -46,7 +49,7 @@ public static void addBack(Deque deque, int value) { Output: 5 */ public static Integer removeFront(Deque deque) { - + deque.removeFirst(); // TODO: Implement this method return null; @@ -61,7 +64,7 @@ public static Integer removeFront(Deque deque) { Output: 15 */ public static Integer removeBack(Deque deque) { - + deque.removeLast(); // TODO: Implement this method return null; @@ -76,10 +79,10 @@ public static Integer removeBack(Deque deque) { Output: 5 */ public static Integer peekFront(Deque deque) { - + return deque.peekFirst(); // TODO: Implement this method - return null; + } /* @@ -91,10 +94,10 @@ public static Integer peekFront(Deque deque) { Output: 15 */ public static Integer peekBack(Deque deque) { - + return deque.peekLast(); // TODO: Implement this method - return null; + } } diff --git a/src/main/java/Sets/HashSet/HashSetProblems.java b/src/main/java/Sets/HashSet/HashSetProblems.java index a36c570..7f63419 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; @@ -19,7 +20,7 @@ public static void main(String[] args) { Output: {"apple"} */ public static void addElement(Set set, String value) { - + set.add(value); // TODO: Implement this method } @@ -33,10 +34,10 @@ public static void addElement(Set set, String value) { Output: true or false */ public static boolean containsValue(Set set, String value) { - + return set.contains(value); // TODO: Implement this method - return false; + } /* @@ -48,7 +49,7 @@ public static boolean containsValue(Set set, String value) { Output: value removed */ public static void removeValue(Set set, String value) { - + set.remove(value); // TODO: Implement this method } @@ -62,10 +63,10 @@ public static void removeValue(Set set, String value) { Output: 2 */ public static int getUniqueCount(Set set) { - + return set.size(); // TODO: Implement this method - return 0; + } /* @@ -77,9 +78,15 @@ public static int getUniqueCount(Set set) { Output: {1,2,3} */ public static Set getUniqueValues(List numbers) { + Set unique = new HashSet<>(); //had to import java Hashset + for (Integer num : numbers) { + unique.add(num); + } + + return unique; // TODO: Implement this method - return null; + } }