diff --git a/src/main/java/Collections/Practice/CollectionBasics.java b/src/main/java/Collections/Practice/CollectionBasics.java index e45cb49..f1f1662 100644 --- a/src/main/java/Collections/Practice/CollectionBasics.java +++ b/src/main/java/Collections/Practice/CollectionBasics.java @@ -1,7 +1,6 @@ package Collections.Practice; -import java.util.ArrayList; -import java.util.Collection; +import java.util.*; public class CollectionBasics { public static void main(String[] args) { @@ -10,6 +9,7 @@ public static void main(String[] args) { numbers.add(4); numbers.add(8); + //numbers.add(8); numbers.add(15); numbers.add(16); numbers.add(23); @@ -33,7 +33,9 @@ public static int sum(Collection numbers) { // TODO: // Loop through the collection // Add each number to total - +for(Integer sum: numbers){ + total += sum; +} return total; } @@ -49,7 +51,11 @@ public static int countEven(Collection numbers) { // TODO: // Loop through the collection // If the number is even, increase count - +for(Integer even: numbers){ + if(even % 2 == 0){ + count++; + } +} return count; } @@ -65,7 +71,11 @@ public static int findMax(Collection numbers) { // TODO: // Loop through numbers // Update max if current number is larger - +for(Integer newMax: numbers){ +if(newMax > max){ + max = newMax; +} +} return max; } @@ -80,8 +90,9 @@ public static boolean hasDuplicates(Collection numbers) { // TODO: // Hint: // Compare the size of a collection with the size of a Set +Set diffNumbers = new HashSet<>(numbers); - return false; + return diffNumbers.size() !=numbers.size(); } @@ -96,7 +107,11 @@ public static int countOccurrences(Collection numbers, int target) { // TODO: // Loop through numbers // If number equals target, increase count - +for(Integer nextNum: numbers){ + if(nextNum == target){ + count++; + } +} return count; } @@ -113,7 +128,12 @@ public static Collection filterGreaterThanTwenty(Collection nu // TODO: // Loop through numbers // Add numbers greater than 20 to result - +for(Integer overTwenty: numbers){ + if(overTwenty > 20){ + result.add(overTwenty); + } +} return result; + } } diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java index 6c2f4e1..d6d0bee 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -1,5 +1,5 @@ package CollectionsHackerrank; - +import java.util.*; import java.util.List; import java.util.Map; import java.util.Queue; @@ -22,10 +22,9 @@ public static void main(String[] args) { Output: [1,2,3,4,5] */ public static List removeDuplicates(List numbers) { - // TODO: Implement this method - return null; + return new ArrayList<>(new LinkedHashSet<>(numbers)); } /* @@ -37,10 +36,14 @@ public static List removeDuplicates(List numbers) { Output: {1=1, 2=2, 3=3} */ public static Map countFrequency(List numbers) { - // TODO: Implement this method + Map frequency = new HashMap<>(); - return null; + for (Integer num : numbers) { + frequency.put(num, frequency.getOrDefault(num, 0) + 1); + } + + return frequency; } /* @@ -52,8 +55,14 @@ public static Map countFrequency(List numbers) { Output: 5 */ public static Integer firstUnique(List numbers) { - // TODO: Implement this method + Map frequency = countFrequency(numbers); + + for (Integer num : numbers) { + if (frequency.get(num) == 1) { + return num; + } + } return null; } @@ -69,8 +78,19 @@ public static Integer firstUnique(List numbers) { Output: true */ public static boolean twoSum(List numbers, int target) { - // TODO: Implement this method + Set seen = new HashSet<>(); + + for (Integer num : numbers) { + + int complement = target - num; + + if (seen.contains(complement)) { + return true; + } + + seen.add(num); + } return false; } @@ -86,8 +106,7 @@ public static boolean twoSum(List numbers, int target) { public static int countUniqueWords(List words) { // TODO: Implement this method - - return 0; + return new HashSet<>(words).size(); } /* @@ -99,10 +118,18 @@ public static int countUniqueWords(List words) { Output: [4,3,2,1] */ public static Queue reverseQueue(Queue queue) { - // TODO: Implement this method + Stack stack = new Stack<>(); - return null; + while (!queue.isEmpty()) { + stack.push(queue.poll()); + } + + while (!stack.isEmpty()) { + queue.add(stack.pop()); + } + + return queue; } /* @@ -117,10 +144,26 @@ public static Queue reverseQueue(Queue queue) { Output: false */ public static boolean isBalanced(String expression) { - // TODO: Implement this method + Stack stack = new Stack<>(); - return false; + for (char c : expression.toCharArray()) { + + if (c == '(') { + stack.push(c); + } + + if (c == ')') { + + if (stack.isEmpty()) { + return false; + } + + stack.pop(); + } + } + + return stack.isEmpty(); } /* @@ -132,10 +175,21 @@ public static boolean isBalanced(String expression) { Output: 3 */ public static Integer mostFrequent(List numbers) { - // TODO: Implement this method + Map frequency = countFrequency(numbers); - return null; + int maxCount = 0; + Integer result = null; + + for (Map.Entry entry : frequency.entrySet()) { + + if (entry.getValue() > maxCount) { + maxCount = entry.getValue(); + result = entry.getKey(); + } + } + + return result; } /* @@ -152,10 +206,19 @@ public static Integer mostFrequent(List numbers) { } */ public static Map> groupByLength(List words) { - // TODO: Implement this method + Map> map = new HashMap<>(); - return null; + for (String word : words) { + + int length = word.length(); + + map.putIfAbsent(length, new ArrayList<>()); + + map.get(length).add(word); + } + + return map; } /* @@ -169,10 +232,23 @@ public static Map> groupByLength(List words) { Output: 9 */ public static int maxSlidingWindowSum(List numbers, int k) { - // TODO: Implement this method + int maxSum = 0; + int windowSum = 0; + + for (int i = 0; i < numbers.size(); i++) { + + windowSum += numbers.get(i); + + if (i >= k - 1) { + + maxSum = Math.max(maxSum, windowSum); + + windowSum -= numbers.get(i - k + 1); + } + } - return 0; + return maxSum; } } } diff --git a/src/main/java/Iterable/Examples/ForEachLoopDemo.java b/src/main/java/Iterable/Examples/ForEachLoopDemo.java index a6d2198..004bfdc 100644 --- a/src/main/java/Iterable/Examples/ForEachLoopDemo.java +++ b/src/main/java/Iterable/Examples/ForEachLoopDemo.java @@ -18,20 +18,24 @@ public static void main(String[] args) { // TODO: // Use a for-each loop to print each student name + for(String student: students) { - System.out.println("\nPrinting students in uppercase:"); - + System.out.println(student); + } + System.out.println(); // TODO: // Use a for-each loop to print each name in uppercase - - - System.out.println("\nCount the number of students:"); - - int count = 0; + for(String student: students) { + System.out.println(student.toUpperCase()); + } + System.out.println(); // TODO: // Use a for-each loop to count how many students are in the list - + int count = 0; +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..9ee0cfe 100644 --- a/src/main/java/Iterable/Examples/IteratorDemo.java +++ b/src/main/java/Iterable/Examples/IteratorDemo.java @@ -17,7 +17,7 @@ public static void main(String[] args) { System.out.println("Original list:"); System.out.println(numbers); - +//Can you store multiple array lists in an iterator? // Create iterator Iterator iterator = numbers.iterator(); @@ -26,7 +26,10 @@ public static void main(String[] args) { // TODO: // Use iterator.hasNext() and iterator.next() // Print each number - +while(iterator.hasNext()){ + Integer num = iterator.next(); + System.out.println(num); +} System.out.println("\nRemoving odd numbers using Iterator"); @@ -35,7 +38,12 @@ public static void main(String[] args) { // TODO: // Use iterator to remove odd numbers // Remember: use iterator.remove() - +while(iterator.hasNext()){ + Integer 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..eb908cb 100644 --- a/src/main/java/Iterable/Practice/IterableWarmups.java +++ b/src/main/java/Iterable/Practice/IterableWarmups.java @@ -30,7 +30,10 @@ public static int sum(Iterable numbers) { // TODO: // Use a for-each loop to calculate the sum +for(Integer sum: numbers){ + total+=sum; +} return total; } @@ -46,8 +49,12 @@ public static int countEven(Iterable numbers) { // TODO: // Loop through numbers // Increment count if number is even - - return count; +for(Integer even: numbers){ + if(even %2 == 0 ){ + count++; + } +} +return count; } @@ -62,7 +69,11 @@ public static int findMax(Iterable numbers) { // TODO: // Loop through numbers // Update max if current number is larger - +for(Integer num: numbers){ + if(num > max){ + max = num; + } +} return max; } @@ -78,7 +89,12 @@ 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..3eaecd5 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -31,11 +31,15 @@ public static void main(String[] args) { Input: [1,2,3] Output: 6 */ - public static int sum(List nums) { + public static int sum(List nums) { // TODO: Implement this method +int total = 0; + for(Integer num: nums){ + total += num; +} - return 0; + return total; } /* @@ -47,10 +51,15 @@ public static int sum(List nums) { Output: 2 */ public static int countEvens(List nums) { - // TODO: Implement this method +int count = 0; +for(Integer even: nums){ + if(even %2 ==0){ + count++; + } +} - return 0; + return count; } /* @@ -65,9 +74,15 @@ public static int countEvens(List nums) { Output: false */ public static boolean hasDuplicate(List nums) { - // TODO: Implement this method + ListsameNumCheck = new ArrayList<>(); + for(Integer num: nums){ + if(sameNumCheck.contains(num)){ + return true; + } +sameNumCheck.add(num); +} return false; } @@ -80,10 +95,14 @@ public static boolean hasDuplicate(List nums) { Output: 7 */ public static int findMax(List nums) { - +int maxNum = Integer.MIN_VALUE; // TODO: Implement this method - - return 0; +for(Integer num: nums){ + if(num > maxNum){ + maxNum = num; + } +} +return maxNum; } /* @@ -97,9 +116,11 @@ public static int findMax(List nums) { The original list should remain unchanged. */ public static List reverse(List nums) { - +ListreverseList = new ArrayList<>(); // TODO: Implement this method - - return null; +for (int i = nums.size()-1; i >= 0; i-- ){ +reverseList.add(nums.get(i)); +} + return reverseList; } } diff --git a/src/main/java/Lists/LinkedLists/LinkedListProblems.java b/src/main/java/Lists/LinkedLists/LinkedListProblems.java index 178a2ba..580d3c8 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -36,9 +36,8 @@ public static void main(String[] args) { Output: [5,10,20,30] */ public static void addToFront(LinkedList list, int value) { - // TODO: Implement this method - +list.addFirst(value); } /* @@ -50,9 +49,8 @@ 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 +62,8 @@ 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 +75,8 @@ public static void removeFirstElement(LinkedList list) { Output: [10,20] */ public static void removeLastElement(LinkedList list) { - // TODO: Implement this method - +list.removeLast(); } /* @@ -92,10 +88,10 @@ public static void removeLastElement(LinkedList list) { Output: 10 */ public static int getFirstElement(LinkedList list) { - // TODO: Implement this method - return 0; + + return list.getFirst(); } /* @@ -110,6 +106,6 @@ 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..955f303 100644 --- a/src/main/java/Maps/HashMap/HashMapProblems.java +++ b/src/main/java/Maps/HashMap/HashMapProblems.java @@ -33,9 +33,8 @@ 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 +46,9 @@ 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); } /* @@ -64,7 +62,7 @@ public static int getQuantity(Map map, String item) { public static void updateQuantity(Map map, String item, int newQuantity) { // TODO: Implement this method - +map.replace(item,newQuantity); } /* @@ -78,7 +76,7 @@ public static void updateQuantity(Map map, String item, int new public static void removeItem(Map map, String item) { // TODO: Implement this method - +map.remove(item); } /* @@ -90,9 +88,16 @@ public static void removeItem(Map map, String item) { Output: {1=1, 2=2, 3=3} */ public static Map countFrequency(List numbers) { - +Map howManyTimes = new HashMap<>(); // TODO: Implement this method - - return null; +for(Integer nums: numbers){ +if(howManyTimes.containsKey(nums)) { +howManyTimes.put(nums, howManyTimes.get(nums)+1); +} + else{ +howManyTimes.put(nums,1); + } +} +return howManyTimes; } } diff --git a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java index e8bbdc2..712d984 100644 --- a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java +++ b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java @@ -1,5 +1,6 @@ package Maps.LinkedHashMap; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -31,9 +32,8 @@ 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 +45,8 @@ 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,8 +58,8 @@ 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,10 +72,9 @@ public static void removeStudent(Map map, String name) { Output: "Jordan" */ public static String getFirstInserted(Map map) { - // TODO: Implement this method - return null; + return map.keySet().iterator().next(); } /* @@ -89,9 +87,11 @@ 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; +MaphowMany= new HashMap<>(); +for (String word: words){ + howMany.put(word, howMany.getOrDefault(word, 0)+1); +} +return howMany; } } diff --git a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java index dac03cf..abaff7d 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -29,9 +29,8 @@ public static void main(String[] args) { Output: [5] */ public static void addToFront(ArrayDeque deque, int value) { - // TODO: Implement this method - +deque.addFirst(value); } /* @@ -43,9 +42,8 @@ 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,9 +55,8 @@ 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 +68,8 @@ public static void removeFront(ArrayDeque deque) { Output: [5,10] */ public static void removeBack(ArrayDeque deque) { - // TODO: Implement this method - +deque.removeLast(); } /* @@ -85,10 +81,9 @@ 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 +95,8 @@ 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..eac8902 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -18,9 +18,8 @@ public static void main(String[] args) { Output: [5] */ public static void addFront(Deque deque, int value) { - // TODO: Implement this method - +deque.addFirst(value); } /* @@ -34,7 +33,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 +48,7 @@ public static Integer removeFront(Deque deque) { // TODO: Implement this method - return null; + return deque.removeFirst(); } /* @@ -64,7 +63,7 @@ public static Integer removeBack(Deque deque) { // TODO: Implement this method - return null; + return deque.removeLast(); } /* @@ -79,7 +78,7 @@ public static Integer peekFront(Deque deque) { // TODO: Implement this method - return null; + return deque.peekFirst(); } /* @@ -94,7 +93,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..992fe09 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,9 +20,8 @@ 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 +33,9 @@ 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 +47,8 @@ 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 +60,9 @@ 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,8 @@ 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); } }