diff --git a/src/main/java/Collections/Practice/CollectionBasics.java b/src/main/java/Collections/Practice/CollectionBasics.java index e45cb49..2f11069 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) { @@ -33,7 +32,9 @@ public static int sum(Collection numbers) { // TODO: // Loop through the collection // Add each number to total - + for(int num: numbers) { + total += num; + } return total; } @@ -50,6 +51,11 @@ public static int countEven(Collection numbers) { // Loop through the collection // If the number is even, increase count + for(int num: numbers) { + if(num % 2 == 0) { + count++; + } + } return count; } @@ -66,6 +72,11 @@ public static int findMax(Collection numbers) { // Loop through numbers // Update max if current number is larger + for(int num: numbers) { + if(num > max) { + max = num; + } + } return max; } @@ -80,6 +91,14 @@ public static boolean hasDuplicates(Collection numbers) { // TODO: // Hint: // Compare the size of a collection with the size of a Set + HashSet set = new HashSet<>(); + + for(int num: numbers) { + //if false, then return true + if(!set.add(num)) { + return true; + } + } return false; } @@ -97,6 +116,9 @@ public static int countOccurrences(Collection numbers, int target) { // Loop through numbers // If number equals target, increase count + for(int num : numbers) { + if(num == target) count++; + } return count; } @@ -114,6 +136,14 @@ public static Collection filterGreaterThanTwenty(Collection nu // Loop through numbers // Add numbers greater than 20 to result + Iterator iterator = numbers.iterator(); + + while (iterator.hasNext()) { + int element = iterator.next(); + if (element > 20) { + result.add(element); + } + } return result; } } diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java index 6c2f4e1..595623c 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -1,8 +1,6 @@ package CollectionsHackerrank; -import java.util.List; -import java.util.Map; -import java.util.Queue; +import java.util.*; public class CollectionsHackerrankProblems { public class CollectionsHackerrankPractice { @@ -11,6 +9,17 @@ public static void main(String[] args) { // You can test your methods here + List numbers = new ArrayList<>(); + + numbers.add(4); + numbers.add(8); + numbers.add(15); + numbers.add(16); + numbers.add(23); + numbers.add(42); + numbers.add(4); + + System.out.println("Remove Duplicates: " + removeDuplicates(numbers)); } /* @@ -24,8 +33,7 @@ public static void main(String[] args) { public static List removeDuplicates(List numbers) { // TODO: Implement this method - - return null; + return (List) new HashSet<>(numbers); } /* @@ -39,8 +47,19 @@ public static List removeDuplicates(List numbers) { public static Map countFrequency(List numbers) { // TODO: Implement this method - - return null; + HashMap map = new HashMap<>(); + + for (int i = 0; i < numbers.size(); i++) { + int num = numbers.get(i); + + if(map.containsKey(num)) { + map.put(num, map.get(num) + 1); + } + else { + map.put(num, 1); + } + } + return map; } /* @@ -54,7 +73,26 @@ public static Map countFrequency(List numbers) { public static Integer firstUnique(List numbers) { // TODO: Implement this method - + LinkedHashMap numberMap = new LinkedHashMap<>(); + + //populate hashmap + for(int num : numbers) { + if(numberMap.containsKey(num)) { + numberMap.put(num, numberMap.get(num) + 1); + } + else { + numberMap.put(num, 1); + } + } + + + //find first unique num in LinkedHashMap (ordered by insertion) + for(int num : numberMap.keySet()) { + //1 means only appeared once, value is set to the # frequency in my hashMap + if(numberMap.get(num) <= 1) { + return num; + } + } return null; } @@ -71,6 +109,43 @@ public static Integer firstUnique(List numbers) { public static boolean twoSum(List numbers, int target) { // TODO: Implement this method + //Bad way, check list of numbers for every number we iterate through O(n^2) + /* + for(int i = 0; i < numbers.size(); i++) { + for(int j = 0; j < numbers.size(); j++) { + if(numbers.get(i) + numbers.get(j) == target) + { + return true; + } + } + } + */ + + + //Good (decent) way, + //build dict of known values, dont need to worry about value, + //want O(1) access of key! + + HashMap numMap = new HashMap<>(); + + for(int num : numbers) { + numMap.put(num, 1); + } + + //iterate through array, + // originally we did arr[i] + arr[j] = target + //but I want to solve for arr[j] now. + + //so its now gonna be abs(arr[i] - target) = arr[j] + for (int i = 0; i < numbers.size(); i++) { + + int sumNeededValue = Math.abs(numbers.get(i) - target); + //if this value that we need for the target is + // somewhere in our list, return true. + if(numMap.containsKey(sumNeededValue)) { + return true; + } + } return false; } @@ -87,7 +162,33 @@ public static int countUniqueWords(List words) { // TODO: Implement this method - return 0; + int count = 0; + //make hashMap + //iterate through list + //if not in hashmap, add to counter + + HashMap wordMap = new HashMap<>(); + + //Iterate through words, add to Map + for(String word : words) { + if (wordMap.containsKey(word)) { + wordMap.put(word, wordMap.get(word) + 1); + } + else { + wordMap.put(word, 1); + } + } + + //then if its 1, increment counter; + for(int i = 0; i < words.size(); i++) { + String word = words.get(i); + + if(wordMap.get(word) <= 1 ) { + count++; + } + } + + return count; } /* @@ -102,7 +203,21 @@ public static Queue reverseQueue(Queue queue) { // TODO: Implement this method - return null; + //convert queue to array + ArrayList newList = new ArrayList<>(); + + //populate array with queue values + for(int i = 0; i < queue.size(); i++) { + newList.add(queue.poll()); + } + + //reverse list + newList.reversed(); + + //then back to Queue (LinkedList, cause Queue is an interface) + Queue resultQueue = new LinkedList<>(newList); + + return resultQueue; } /* @@ -120,7 +235,20 @@ public static boolean isBalanced(String expression) { // TODO: Implement this method - return false; + int lCount = 0; + int rCount = 0; + + //Count left parenthesis and right parenthesis, if they have equal count + //then they are balanced. + for(int i = 0; i < expression.length(); i++) { + if(expression.charAt(i) == '(') { + lCount++; + } + else if (expression.charAt(i) == ')') { + rCount++; + } + } + return lCount == rCount; } /* @@ -135,7 +263,34 @@ public static Integer mostFrequent(List numbers) { // TODO: Implement this method - return null; + + HashMap frequencyMap = new HashMap<>(); + + //Build HashMap, based on number frequency + for(int num : numbers) { + if(frequencyMap.containsKey(num)) { + frequencyMap.put(num, frequencyMap.get(num) + 1); + } + else { + frequencyMap.put(num, 1); + } + } + + int maxValue = 0; + int mostFrequentNum = 0; + + //Iterate through HashMap keys, find which one has the largest value + //return that value. + + for(int key : frequencyMap.keySet()) { + //Max pattern + if(frequencyMap.get(key) > maxValue) { + maxValue = frequencyMap.get(key); + mostFrequentNum = key; + } + } + + return mostFrequentNum; } /* @@ -155,7 +310,25 @@ public static Map> groupByLength(List words) { // TODO: Implement this method - return null; + HashMap> lenGroup = new HashMap<>(); + + //build map. + for(String word: words) { + + //Key is now the length + if(lenGroup.containsKey(word.length())) { + List getList = lenGroup.get(word.length()); + getList.add(word); + lenGroup.put(word.length(), getList); + } + else { + List wordList = new ArrayList<>(); + wordList.add(word); + lenGroup.put(word.length(),wordList); + } + + } + return lenGroup; } /* @@ -172,7 +345,55 @@ public static int maxSlidingWindowSum(List numbers, int k) { // TODO: Implement this method - return 0; + //initialize variables + int l = 0; + int r = 0; + + int maxSum = 0; + int sum = 0; + boolean rWindowReached = false; + + //minus the slided value, keep sum otherwise. + //like a train. + + //while loop + while(r < numbers.size()) { + + if(!rWindowReached) { + //ad r value to sum, until r reaches k-1 + sum += r; + r++; + + //check if r reaches k-1 + if(r >= k-1) { + rWindowReached = true; + + //If new maxSum found, set it + if(sum >= maxSum) { + maxSum = sum; + } + } + } + else { + + //subtract left side of window + sum -= numbers.get(l); + l++; + + //add right side of window + sum += numbers.get(r); + r++; + + //If new maxSum found, set it + if(sum >= maxSum) { + maxSum = sum; + } + + } + + } + + return maxSum; } } } diff --git a/src/main/java/Iterable/Practice/IterableWarmups.java b/src/main/java/Iterable/Practice/IterableWarmups.java index 9e9de94..4be5c5a 100644 --- a/src/main/java/Iterable/Practice/IterableWarmups.java +++ b/src/main/java/Iterable/Practice/IterableWarmups.java @@ -31,6 +31,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; } @@ -47,6 +50,10 @@ public static int countEven(Iterable numbers) { // Loop through numbers // Increment count if number is even + for(int num : numbers) { + if(num % 2 == 0) count++; + } + return count; } @@ -63,6 +70,11 @@ public static int findMax(Iterable numbers) { // Loop through numbers // Update max if current number is larger + for(int num : numbers) { + if(num > max) { + max = num; + } + } return max; } @@ -75,6 +87,12 @@ public static int countMatches(Iterable words, String target) { int count = 0; + for(String word : words) { + if(word.equals(target)) { + count++; + } + } + // TODO: // 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..ccba9a0 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -2,6 +2,7 @@ import java.util.ArrayList; +import java.util.HashSet; import java.util.List; public class ArrayListProblems { @@ -34,8 +35,11 @@ public static void main(String[] args) { public static int sum(List nums) { // TODO: Implement this method - - return 0; + int sum = 0; + for(int num : nums) { + sum += num; + } + return sum; } /* @@ -49,8 +53,13 @@ public static int sum(List nums) { public static int countEvens(List nums) { // TODO: Implement this method - - return 0; + int count = 0; + for(int num : nums) { + if(num % 2 == 0) { + count += num; + } + } + return count; } /* @@ -67,7 +76,12 @@ public static int countEvens(List nums) { public static boolean hasDuplicate(List nums) { // TODO: Implement this method - + HashSet numSet = new HashSet(); + for(int num : nums) { + if(!numSet.add(num)) { + return true; + } + } return false; } @@ -82,8 +96,15 @@ public static boolean hasDuplicate(List nums) { public static int findMax(List nums) { // TODO: Implement this method + int max = Integer.MIN_VALUE; - return 0; + for(int num : nums) { + if(num > max) { + num = max; + } + } + + return max; } /* @@ -99,7 +120,6 @@ public static int findMax(List nums) { public static List reverse(List nums) { // TODO: Implement this method - - return null; + return new ArrayList(nums.reversed()); } } diff --git a/src/main/java/Lists/LinkedLists/LinkedListProblems.java b/src/main/java/Lists/LinkedLists/LinkedListProblems.java index 178a2ba..146036c 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -38,6 +38,14 @@ public static void main(String[] args) { public static void addToFront(LinkedList list, int value) { // TODO: Implement this method + //make new list with new value at the head + LinkedList newList = new LinkedList<>(); + newList.push(value); + + //Add everything else to new list + for(int item : list) { + newList.push(item); + } } @@ -52,7 +60,7 @@ public static void addToFront(LinkedList list, int value) { public static void addToEnd(LinkedList list, int value) { // TODO: Implement this method - + list.push(value); } /* @@ -66,7 +74,7 @@ public static void addToEnd(LinkedList list, int value) { public static void removeFirstElement(LinkedList list) { // TODO: Implement this method - + list.removeFirst(); } /* @@ -80,7 +88,7 @@ public static void removeFirstElement(LinkedList list) { public static void removeLastElement(LinkedList list) { // TODO: Implement this method - + list.pop(); } /* @@ -94,8 +102,7 @@ public static void removeLastElement(LinkedList list) { public static int getFirstElement(LinkedList list) { // TODO: Implement this method - - return 0; + return list.getFirst(); } /* @@ -110,6 +117,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..c21a102 100644 --- a/src/main/java/Maps/HashMap/HashMapProblems.java +++ b/src/main/java/Maps/HashMap/HashMapProblems.java @@ -35,6 +35,7 @@ public static void main(String[] args) { public static void addItem(Map map, String item, int quantity) { // TODO: Implement this method + map.put(item, quantity); } @@ -49,8 +50,7 @@ public static void addItem(Map map, String item, int quantity) public static int getQuantity(Map map, String item) { // TODO: Implement this method - - return 0; + return map.get(item); } /* @@ -64,6 +64,7 @@ public static int getQuantity(Map map, String item) { public static void updateQuantity(Map map, String item, int newQuantity) { // TODO: Implement this method + map.put(item, newQuantity); } @@ -78,6 +79,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); } @@ -92,7 +94,18 @@ public static void removeItem(Map map, String item) { public static Map countFrequency(List numbers) { // TODO: Implement this method - - return null; + HashMap map = new HashMap<>(); + + for(int num : numbers) { + if(map.containsKey(num)) { + //Increment by 1 + map.put(num, map.get(num) + 1); + } + else { + map.put(num, 1); + } + } + + return map; } } diff --git a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java index e8bbdc2..2bfc9f4 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.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -33,7 +34,7 @@ public static void main(String[] args) { public static void addStudent(Map map, String name, int grade) { // TODO: Implement this method - + map.put(name, grade); } /* @@ -47,6 +48,7 @@ public static void addStudent(Map map, String name, int grade) public static void updateGrade(Map map, String name, int newGrade) { // TODO: Implement this method + map.put(name, newGrade); } @@ -61,6 +63,7 @@ public static void updateGrade(Map map, String name, int newGra public static void removeStudent(Map map, String name) { // TODO: Implement this method + map.remove(name); } @@ -76,7 +79,8 @@ public static String getFirstInserted(Map map) { // TODO: Implement this method - return null; + List orderedKeys = new ArrayList<>(map.keySet()); + return orderedKeys.getFirst(); } /* @@ -92,6 +96,16 @@ public static Map wordFrequency(List words) { // TODO: Implement this method - return null; + LinkedHashMap linkedHashMap = new LinkedHashMap<>(); + + for(String word : linkedHashMap.keySet()) { + if(linkedHashMap.get(word) != null) { + linkedHashMap.put(word, linkedHashMap.get(word) + 1); + } + else { + linkedHashMap.put(word, 1); + } + } + return linkedHashMap; } } diff --git a/src/main/java/Maps/TreeMap/TreeMapProblems.java b/src/main/java/Maps/TreeMap/TreeMapProblems.java index b101d2e..3b90da3 100644 --- a/src/main/java/Maps/TreeMap/TreeMapProblems.java +++ b/src/main/java/Maps/TreeMap/TreeMapProblems.java @@ -1,6 +1,6 @@ package Maps.TreeMap; -import java.util.TreeMap; +import java.util.*; public class TreeMapProblems { public static void main(String[] args) { @@ -31,6 +31,7 @@ public static void main(String[] args) { public static void addPlayer(TreeMap map, int rank, String name) { // TODO: Implement this method + map.put(rank, name); } @@ -46,7 +47,8 @@ public static String getTopPlayer(TreeMap map) { // TODO: Implement this method - return null; + List keyList = new ArrayList<>(map.keySet()); + return map.get(keyList.getFirst()); } /* @@ -60,8 +62,8 @@ Return the player with the lowest ranking (largest key). public static String getLowestPlayer(TreeMap map) { // TODO: Implement this method - - return null; + List keyList = new ArrayList<>(map.keySet()); + return map.get(keyList.getLast()); } /* @@ -75,7 +77,7 @@ public static String getLowestPlayer(TreeMap map) { public static void removePlayer(TreeMap map, int rank) { // TODO: Implement this method - + map.remove(rank); } /* @@ -89,7 +91,21 @@ public static void removePlayer(TreeMap map, int rank) { public static Integer getNextRank(TreeMap map, int rank) { // TODO: Implement this method + Iterator> iterator = map.entrySet().iterator(); + + boolean rankFound = false; + + while(iterator.hasNext()) { + Map.Entry entry = iterator.next(); + if(rankFound) { + return entry.getKey(); + } + + if(iterator.next().getKey().equals(rank)) { + rankFound = true; + } + } - return null; + return -1; } } diff --git a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java index dac03cf..337578d 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -32,6 +32,7 @@ public static void addToFront(ArrayDeque deque, int value) { // TODO: Implement this method + } /* @@ -45,6 +46,7 @@ public static void addToFront(ArrayDeque deque, int value) { public static void addToBack(ArrayDeque deque, int value) { // TODO: Implement this method + deque.addLast(value); } @@ -59,7 +61,7 @@ public static void addToBack(ArrayDeque deque, int value) { public static void removeFront(ArrayDeque deque) { // TODO: Implement this method - + deque.removeFirst(); } /* @@ -73,7 +75,7 @@ public static void removeFront(ArrayDeque deque) { public static void removeBack(ArrayDeque deque) { // TODO: Implement this method - + deque.removeLast(); } /* @@ -88,7 +90,7 @@ public static Integer peekFront(ArrayDeque deque) { // TODO: Implement this method - return null; + return deque.peekFirst(); } /* @@ -103,6 +105,6 @@ public static Integer peekBack(ArrayDeque deque) { // TODO: Implement this method - return null; + return deque.peekLast(); } } diff --git a/src/main/java/Queues/Deque/DequeProblems.java b/src/main/java/Queues/Deque/DequeProblems.java index 7ef0c06..11437eb 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -20,7 +20,7 @@ public static void main(String[] args) { public static void addFront(Deque deque, int value) { // TODO: Implement this method - + deque.addFirst(value); } /* @@ -34,7 +34,7 @@ public static void addFront(Deque deque, int value) { public static void addBack(Deque deque, int value) { // TODO: Implement this method - + deque.addLast(value); } /* @@ -48,8 +48,7 @@ public static void addBack(Deque deque, int value) { public static Integer removeFront(Deque deque) { // TODO: Implement this method - - return null; + return deque.removeFirst(); } /* @@ -63,8 +62,7 @@ public static Integer removeFront(Deque deque) { public static Integer removeBack(Deque deque) { // TODO: Implement this method - - return null; + return deque.removeLast(); } /* @@ -78,8 +76,7 @@ public static Integer removeBack(Deque deque) { public static Integer peekFront(Deque deque) { // TODO: Implement this method - - return null; + return deque.peekFirst(); } /* @@ -93,8 +90,7 @@ public static Integer peekFront(Deque deque) { 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..f345c40 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; @@ -21,6 +22,7 @@ public static void main(String[] args) { public static void addElement(Set set, String value) { // TODO: Implement this method + set.add(value); } @@ -35,8 +37,7 @@ public static void addElement(Set set, String value) { public static boolean containsValue(Set set, String value) { // TODO: Implement this method - - return false; + return set.contains(value); } /* @@ -50,6 +51,7 @@ public static boolean containsValue(Set set, String value) { public static void removeValue(Set set, String value) { // TODO: Implement this method + set.remove(value); } @@ -64,8 +66,12 @@ public static void removeValue(Set set, String value) { public static int getUniqueCount(Set set) { // TODO: Implement this method + int count = 0; + for(String item : set) { + count++; + } - return 0; + return count; } /* @@ -79,7 +85,7 @@ public static int getUniqueCount(Set set) { public static Set getUniqueValues(List numbers) { // TODO: Implement this method - - return null; + Set set = new HashSet<>(numbers); + return set; } }