diff --git a/src/main/java/Collections/Practice/CollectionBasics.java b/src/main/java/Collections/Practice/CollectionBasics.java index e45cb49..c5743ed 100644 --- a/src/main/java/Collections/Practice/CollectionBasics.java +++ b/src/main/java/Collections/Practice/CollectionBasics.java @@ -33,6 +33,9 @@ public static int sum(Collection numbers) { // TODO: // Loop through the collection // Add each number to total + for(Integer number : numbers){ + total+= number; + } return total; } @@ -49,6 +52,11 @@ public static int countEven(Collection numbers) { // TODO: // Loop through the collection // If the number is even, increase count + for(Integer number : numbers){ + if(number % 2 == 0){ + count ++; + } + } return count; } @@ -65,6 +73,9 @@ public static int findMax(Collection numbers) { // TODO: // Loop through numbers // Update max if current number is larger + for (Integer number : numbers){ + max = Math.max(max,number); + } return max; } @@ -80,11 +91,23 @@ public static boolean hasDuplicates(Collection numbers) { // TODO: // Hint: // Compare the size of a collection with the size of a Set + for (Integer a : numbers) { + int count = 0; + + for (Integer b : numbers) { + if (a.equals(b)) { + count++; + } + + if (count > 1) { + return true; + } + } + } return false; } - /* PROBLEM 5 Count how many times a target value appears @@ -92,12 +115,18 @@ public static boolean hasDuplicates(Collection numbers) { public static int countOccurrences(Collection numbers, int target) { int count = 0; - + for (Integer a : numbers) { + if (a.equals(target)) { + count++; + } + return count; + } // TODO: // Loop through numbers // If number equals target, increase count return count; + } diff --git a/src/main/java/Collections/Practice/CommonMethodsDemo.java b/src/main/java/Collections/Practice/CommonMethodsDemo.java index b2dd6f9..acd6128 100644 --- a/src/main/java/Collections/Practice/CommonMethodsDemo.java +++ b/src/main/java/Collections/Practice/CommonMethodsDemo.java @@ -131,6 +131,13 @@ public static void main(String[] args) { Add the following values: 10, 20, 30, 40, 50 */ + Collection numbers = new ArrayList<>(); + numbers.add(10); + numbers.add(20); + numbers.add(30); + numbers.add(40); + numbers.add(50); + System.out.println(numbers); /* @@ -138,17 +145,25 @@ public static void main(String[] args) { Print the size of the numbers collection */ + System.out.println("Size collection: "+ numbers.size()); /* TODO 3: Check if the collection contains 30 */ - + for (Integer number: numbers){ + if(numbers.contains(30)){ + System.out.println(true); + break; + } + } /* TODO 4: Remove the number 20 */ + numbers.remove(20); + System.out.println(numbers); /* @@ -156,15 +171,30 @@ public static void main(String[] args) { Loop through the numbers collection and print each value */ + for(Integer number: numbers){ + System.out.println(number); + } /* REFLECTION QUESTIONS: 1. Why can we use Collection as the reference type? + + - Interface Parent class of data structures such as lists, sets, + and queues. + 2. What methods are available because of the Collection interface? + + - add(), remove(), size(), isEmpty();, contains(), iterator(), clear() + 3. What methods are NOT available when using Collection instead of List? + + - get(int index), set(int index, element), add(int index, element), + remove(int index) + */ + } } diff --git a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java index 6c2f4e1..9734a79 100644 --- a/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java +++ b/src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java @@ -1,15 +1,60 @@ package CollectionsHackerrank; -import java.util.List; -import java.util.Map; -import java.util.Queue; +import java.util.*; public class CollectionsHackerrankProblems { - public class CollectionsHackerrankPractice { - public static void main(String[] args) { // You can test your methods here + // Problem 1 Test + List nums1 = new ArrayList<>(Arrays.asList(1,2,2,3,4,4,5)); + System.out.println("Remove Duplicates: " + removeDuplicates(nums1)); + + // Problem 2 Test + List nums2 = new ArrayList<>(Arrays.asList(1,2,2,3,3,3)); + System.out.println("Count Frequency: " + countFrequency(nums2)); + + // Problem 3 Test + List nums3 = new ArrayList<>(Arrays.asList(4,5,1,2,0,4)); + System.out.println("First Unique: " + firstUnique(nums3)); + + // Problem 4 Test + List nums4 = new ArrayList<>(Arrays.asList(2,7,11,15)); + int target = 9; + System.out.println("Two Sum Exists: " + twoSum(nums4, target)); + + // Problem 5 Test + List words1 = new ArrayList<>(Arrays.asList("apple","banana","apple","orange")); + System.out.println("Unique Words Count: " + countUniqueWords(words1)); + + // Problem 6 Test + Queue queue = new ArrayDeque<>(); + queue.add(1); + queue.add(2); + queue.add(3); + queue.add(4); + + System.out.println("Original Queue: " + queue); + System.out.println("Reversed Queue: " + reverseQueue(queue)); + + // Problem 7 Test + String expr1 = "(())"; + String expr2 = "(()"; + System.out.println("Is Balanced ( (()) ): " + isBalanced(expr1)); + System.out.println("Is Balanced ( (() ): " + isBalanced(expr2)); + + // Problem 8 Test + List nums5 = new ArrayList<>(Arrays.asList(1,3,2,3,4,3)); + System.out.println("Most Frequent: " + mostFrequent(nums5)); + + // Problem 9 Test + List words2 = new ArrayList<>(Arrays.asList("cat","dog","elephant","ant")); + System.out.println("Group by Length: " + groupByLength(words2)); + + // Problem 10 Test + List nums6 = new ArrayList<>(Arrays.asList(2,1,5,1,3,2)); + int k = 3; + System.out.println("Max Sliding Window Sum: " + maxSlidingWindowSum(nums6, k)); } @@ -24,8 +69,9 @@ public static void main(String[] args) { public static List removeDuplicates(List numbers) { // TODO: Implement this method + HashSet set = new HashSet<>(numbers); - return null; + return new ArrayList<>(set); } /* @@ -39,8 +85,12 @@ public static List removeDuplicates(List numbers) { public static Map countFrequency(List numbers) { // TODO: Implement this method + Map map = new HashMap<>(); + for (Integer number : numbers) { + map.put(number, map.getOrDefault(number, 0) + 1); + } - return null; + return map; } /* @@ -54,6 +104,23 @@ public static Map countFrequency(List numbers) { public static Integer firstUnique(List numbers) { // TODO: Implement this method + Map map = new HashMap<>(); + for (Integer number : numbers) { + map.put(number, map.getOrDefault(number, 0) + 1); + + } + // next, return the first key that value = 1 + // num = numbers list + // 4 insert key check value, no. + // 5 Insert key check value, yes. + for (Integer num : numbers){ + if(map.get(num) == 1){ + return num; + + } + + + } return null; } @@ -71,8 +138,24 @@ public static Integer firstUnique(List numbers) { public static boolean twoSum(List numbers, int target) { // TODO: Implement this method + HashSet set = new HashSet<>(); + + int complement = 0; + + for (Integer num : numbers) { + complement = target - num; + + if (set.contains(complement)) { + return true; + } + + set.add(num); + + } + return false; + } /* @@ -86,8 +169,10 @@ public static boolean twoSum(List numbers, int target) { public static int countUniqueWords(List words) { // TODO: Implement this method + HashSet set = new HashSet<>(words); + + return set.size(); - return 0; } /* @@ -101,8 +186,17 @@ public static int countUniqueWords(List words) { public static Queue reverseQueue(Queue queue) { // TODO: Implement this method + Stack stack = new Stack<>(); + Queue reverse = new ArrayDeque<>(); + while (!(queue.isEmpty())) { + stack.push(queue.poll()); + + } + while (!(stack.isEmpty())){ + reverse.add(stack.pop()); + } + return reverse; - return null; } /* @@ -119,8 +213,23 @@ public static Queue reverseQueue(Queue queue) { public static boolean isBalanced(String expression) { // TODO: Implement this method + Stack stack = new Stack<>(); + for (int i = 0; i < expression.length(); i++) { + char ch = expression.charAt(i); + + if (ch == '(') { + stack.push(ch); + } + else if (ch == ')') { + if (stack.isEmpty()) { + return false; + } + stack.pop(); + } + } + + return stack.isEmpty(); - return false; } /* @@ -134,6 +243,19 @@ public static boolean isBalanced(String expression) { public static Integer mostFrequent(List numbers) { // TODO: Implement this method + int max = 0; + HashMap map = new HashMap<>(); + for(Integer num : numbers){ + map.put(num, map.getOrDefault(num, 0)+1); + max = Math.max(max, map.get(num)); + } + + for(Integer number: numbers){ + + if (max == map.get(number)){ + return number; + } + } return null; } @@ -154,8 +276,22 @@ public static Integer mostFrequent(List numbers) { public static Map> groupByLength(List words) { // TODO: Implement this method + HashMap> map = new HashMap<>(); + for(String word : words){ + Integer length = word.length(); + + if(!(map.containsKey(length))){ + map.put(length, new ArrayList<>()); + } + + map.get(length).add(word); + + + } + + + return map; - return null; } /* @@ -171,8 +307,22 @@ public static Map> groupByLength(List words) { public static int maxSlidingWindowSum(List numbers, int k) { // TODO: Implement this method + int windowSum = 0; + for (int i = 0; i < k; i++) { + windowSum += numbers.get(i); + } + + int maxSum = windowSum; + + // 2) slide the window + for (int right = k; right < numbers.size(); right++) { + int left = right - k; // index leaving the window + windowSum = windowSum - numbers.get(left) + numbers.get(right); + maxSum = Math.max(maxSum, windowSum); + } + + return maxSum; - return 0; } } -} + diff --git a/src/main/java/Iterable/Examples/ForEachLoopDemo.java b/src/main/java/Iterable/Examples/ForEachLoopDemo.java index a6d2198..9eaa97d 100644 --- a/src/main/java/Iterable/Examples/ForEachLoopDemo.java +++ b/src/main/java/Iterable/Examples/ForEachLoopDemo.java @@ -17,12 +17,18 @@ public static void main(String[] args) { // TODO: // Use a for-each loop to print each student name + for(String student : students){ + System.out.println(student); + } System.out.println("\nPrinting students in uppercase:"); // TODO: // Use a for-each loop to print each name in uppercase + for(String student : students){ + System.out.println(student.toUpperCase()); + } System.out.println("\nCount the number of students:"); @@ -31,6 +37,9 @@ public static void main(String[] args) { // TODO: // Use a for-each loop to count how many students are in the list + for(String student : students){ + count++; + } System.out.println("Total students: " + count); } diff --git a/src/main/java/Iterable/Examples/IteratorDemo.java b/src/main/java/Iterable/Examples/IteratorDemo.java index b09fca7..1eaa9ba 100644 --- a/src/main/java/Iterable/Examples/IteratorDemo.java +++ b/src/main/java/Iterable/Examples/IteratorDemo.java @@ -26,6 +26,13 @@ public static void main(String[] args) { // TODO: // Use iterator.hasNext() and iterator.next() // Print each number + while(iterator.hasNext()){ + System.out.println(iterator.next()); + } + + + + System.out.println("\nRemoving odd numbers using Iterator"); @@ -34,8 +41,12 @@ public static void main(String[] args) { // TODO: // Use iterator to remove odd numbers - // Remember: use iterator.remove() - + // Remember: use iterator.remove + while(iterator.hasNext()){ + if(! (iterator.next() % 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..89165a5 100644 --- a/src/main/java/Iterable/Practice/IterableWarmups.java +++ b/src/main/java/Iterable/Practice/IterableWarmups.java @@ -31,6 +31,10 @@ public static int sum(Iterable numbers) { // TODO: // Use a for-each loop to calculate the sum + for(Integer number : numbers){ + total += number; + } + return total; } @@ -46,11 +50,17 @@ public static int countEven(Iterable numbers) { // TODO: // Loop through numbers // Increment count if number is even + for(Integer number : numbers){ + if(number % 2 == 0){ + count += number; + } + } return count; } + /* PROBLEM 3 Return the maximum value @@ -62,6 +72,9 @@ public static int findMax(Iterable numbers) { // TODO: // Loop through numbers // Update max if current number is larger + for(Integer number : numbers){ + max = Math.max(max,number); + } return max; } @@ -79,6 +92,8 @@ public static int countMatches(Iterable words, String target) { // Loop through words // Compare each word to target + + return count; } } diff --git a/src/main/java/Lists/ArrayLists/ArrayListProblems.java b/src/main/java/Lists/ArrayLists/ArrayListProblems.java index baf4dfa..e728ff0 100644 --- a/src/main/java/Lists/ArrayLists/ArrayListProblems.java +++ b/src/main/java/Lists/ArrayLists/ArrayListProblems.java @@ -32,10 +32,13 @@ public static void main(String[] args) { Output: 6 */ public static int sum(List nums) { - + int sum = 0; // TODO: Implement this method + for(Integer num : nums){ + sum += num; + } - return 0; + return sum; } /* @@ -47,10 +50,15 @@ public static int sum(List nums) { Output: 2 */ public static int countEvens(List nums) { - + int count = 0; // TODO: Implement this method + for(Integer num : nums){ + if(num % 2 == 0){ + count += 1; + } + } - return 0; + return count; } /* @@ -67,6 +75,19 @@ public static int countEvens(List nums) { public static boolean hasDuplicate(List nums) { // TODO: Implement this method + for (Integer a : nums) { + int count = 0; + + for (Integer b : nums) { + if (a.equals(b)) { + count++; + } + + if (count > 1) { + return true; + } + } + } return false; } @@ -81,11 +102,17 @@ public static boolean hasDuplicate(List nums) { */ public static int findMax(List nums) { + int max = 0; // TODO: Implement this method + for(Integer num : nums){ + max = Math.max(max,num); + } - return 0; + return max; } + + /* Problem 5 Return a NEW list that contains the elements of the original list in reverse order. @@ -97,9 +124,14 @@ public static int findMax(List nums) { The original list should remain unchanged. */ public static List reverse(List nums) { - + ArrayList reverseList = new ArrayList<>(nums.size() -1); + int counter = 0; // TODO: Implement this method + for(int i = nums.size()-1 ; i>0 ; i --){ + reverseList.add(counter, nums.get(i)); + counter ++; + } - return null; + return reverseList; } } diff --git a/src/main/java/Lists/LinkedLists/LinkedListProblems.java b/src/main/java/Lists/LinkedLists/LinkedListProblems.java index 178a2ba..7277175 100644 --- a/src/main/java/Lists/LinkedLists/LinkedListProblems.java +++ b/src/main/java/Lists/LinkedLists/LinkedListProblems.java @@ -38,6 +38,8 @@ public static void main(String[] args) { public static void addToFront(LinkedList list, int value) { // TODO: Implement this method + list.addFirst(5); + System.out.println(list); } @@ -52,6 +54,7 @@ public static void addToFront(LinkedList list, int value) { public static void addToEnd(LinkedList list, int value) { // TODO: Implement this method + list.addLast(40); } @@ -66,6 +69,7 @@ public static void addToEnd(LinkedList list, int value) { public static void removeFirstElement(LinkedList list) { // TODO: Implement this method + list.removeFirst(); } @@ -80,6 +84,7 @@ public static void removeFirstElement(LinkedList list) { public static void removeLastElement(LinkedList list) { // TODO: Implement this method + list.removeLast(); } @@ -92,10 +97,9 @@ public static void removeLastElement(LinkedList list) { Output: 10 */ public static int getFirstElement(LinkedList list) { - // TODO: Implement this method - return 0; + return list.get(0); } /* @@ -110,6 +114,6 @@ public static int getLastElement(LinkedList list) { // TODO: Implement this method - return 0; + return list.get(list.size()-1); } } diff --git a/src/main/java/Maps/HashMap/HashMapProblems.java b/src/main/java/Maps/HashMap/HashMapProblems.java index 04b5567..1662cc1 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); } @@ -50,7 +51,7 @@ public static int getQuantity(Map map, String item) { // TODO: Implement this method - return 0; + return map.get(item); } /* @@ -64,7 +65,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,11 @@ public static void removeItem(Map map, String item) { public static Map countFrequency(List numbers) { // TODO: Implement this method + Map frequent = new HashMap<>(); + for(Integer num : numbers){ + frequent.put(num,frequent.getOrDefault(num, 0)+1); + } - return null; + return frequent; } } diff --git a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java index e8bbdc2..45038ed 100644 --- a/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java +++ b/src/main/java/Maps/LinkedHashMap/LinkedHashMapProblems.java @@ -1,8 +1,6 @@ package Maps.LinkedHashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class LinkedHashMapProblems { public static void main(String[] args) { @@ -33,6 +31,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 +46,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 +61,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); } @@ -73,10 +74,11 @@ public static void removeStudent(Map map, String name) { Output: "Jordan" */ public static String getFirstInserted(Map map) { - // TODO: Implement this method + Set keys = map.keySet(); - return null; + Iterator iterator = keys.iterator(); + return iterator.next(); } /* @@ -91,7 +93,12 @@ public static String getFirstInserted(Map map) { public static Map wordFrequency(List words) { // TODO: Implement this method + Map frequent = new LinkedHashMap<>(); + + for(String items : words){ + frequent.put(items,frequent.getOrDefault(items, 0)+1); + } - return null; + return frequent; } } diff --git a/src/main/java/Maps/TreeMap/TreeMapProblems.java b/src/main/java/Maps/TreeMap/TreeMapProblems.java index b101d2e..e87ebdb 100644 --- a/src/main/java/Maps/TreeMap/TreeMapProblems.java +++ b/src/main/java/Maps/TreeMap/TreeMapProblems.java @@ -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,7 @@ public static String getTopPlayer(TreeMap map) { // TODO: Implement this method - return null; + return map.get(1); } /* @@ -61,7 +62,7 @@ public static String getLowestPlayer(TreeMap map) { // TODO: Implement this method - return null; + return map.get(map.size()); } /* @@ -75,6 +76,7 @@ public static String getLowestPlayer(TreeMap map) { public static void removePlayer(TreeMap map, int rank) { // TODO: Implement this method + map.remove(rank); } @@ -90,6 +92,6 @@ public static Integer getNextRank(TreeMap map, int rank) { // TODO: Implement this method - return null; + return map.higherKey(rank); } } diff --git a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java index dac03cf..33ee302 100644 --- a/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java +++ b/src/main/java/Queues/ArrayDeque/ArrayDequeProblems.java @@ -31,6 +31,7 @@ public static void main(String[] args) { public static void addToFront(ArrayDeque deque, int value) { // TODO: Implement this method + deque.addFirst(value); } @@ -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,6 +61,7 @@ public static void addToBack(ArrayDeque deque, int value) { public static void removeFront(ArrayDeque deque) { // TODO: Implement this method + deque.removeFirst(); } @@ -73,6 +76,7 @@ public static void removeFront(ArrayDeque deque) { public static void removeBack(ArrayDeque deque) { // TODO: Implement this method + deque.removeLast(); } @@ -88,7 +92,7 @@ public static Integer peekFront(ArrayDeque deque) { // TODO: Implement this method - return null; + return deque.getFirst(); } /* @@ -103,6 +107,6 @@ public static Integer peekBack(ArrayDeque deque) { // TODO: Implement this method - return null; + return deque.getLast(); } } diff --git a/src/main/java/Queues/Deque/DequeProblems.java b/src/main/java/Queues/Deque/DequeProblems.java index 7ef0c06..d0fac8e 100644 --- a/src/main/java/Queues/Deque/DequeProblems.java +++ b/src/main/java/Queues/Deque/DequeProblems.java @@ -1,11 +1,34 @@ package Queues.Deque; +import java.util.ArrayDeque; import java.util.Deque; public class DequeProblems { public static void main(String[] args) { + Deque deque = new ArrayDeque<>(); - // You can test your methods here + // Test addFront + addFront(deque, 5); + System.out.println("After addFront(5): " + deque); + + // Test addBack + addBack(deque, 10); + addBack(deque, 15); + System.out.println("After addBack(10,15): " + deque); + + // Test peekFront + System.out.println("Peek Front: " + peekFront(deque)); + + // Test peekBack + System.out.println("Peek Back: " + peekBack(deque)); + + // Test removeFront + System.out.println("Removed Front: " + removeFront(deque)); + System.out.println("Deque after removeFront: " + deque); + + // Test removeBack + System.out.println("Removed Back: " + removeBack(deque)); + System.out.println("Deque after removeBack: " + deque); } @@ -20,7 +43,7 @@ public static void main(String[] args) { public static void addFront(Deque deque, int value) { // TODO: Implement this method - + deque.addFirst(value); } /* @@ -34,6 +57,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 +72,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 +86,7 @@ public static Integer removeFront(Deque deque) { public static Integer removeBack(Deque deque) { // TODO: Implement this method - - return null; + return deque.removeLast(); } /* @@ -78,8 +100,7 @@ public static Integer removeBack(Deque deque) { public static Integer peekFront(Deque deque) { // TODO: Implement this method - - return null; + return deque.getFirst(); } /* @@ -93,8 +114,7 @@ public static Integer peekFront(Deque deque) { public static Integer peekBack(Deque deque) { // TODO: Implement this method - - return null; + return deque.getLast(); } } diff --git a/src/main/java/Sets/HashSet/HashSetProblems.java b/src/main/java/Sets/HashSet/HashSetProblems.java index a36c570..7bcd40a 100644 --- a/src/main/java/Sets/HashSet/HashSetProblems.java +++ b/src/main/java/Sets/HashSet/HashSetProblems.java @@ -1,12 +1,38 @@ package Sets.HashSet; +import java.sql.Array; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Set; public class HashSetProblems { public static void main(String[] args) { - // You can test your methods here + // + Set fruits = new HashSet<>(); + addElement(fruits, "apple"); + addElement(fruits,"orange"); + addElement(fruits,"grapes"); + addElement(fruits,"pear"); + + + System.out.println(fruits); + + System.out.println(containsValue(fruits, "orange")); + System.out.println(containsValue(fruits, "melon")); + System.out.println(getUniqueCount(fruits)); + + ArrayList numbers = new ArrayList<>(); + numbers.add(1); + numbers.add(2); + numbers.add(2); + numbers.add(1); + numbers.add(3); + + System.out.println(getUniqueValues(numbers)); + + } @@ -21,9 +47,10 @@ public static void main(String[] args) { public static void addElement(Set set, String value) { // TODO: Implement this method - + set.add(value); } + /* Problem 2 Check if the set contains a value. @@ -33,8 +60,10 @@ public static void addElement(Set set, String value) { Output: true or false */ public static boolean containsValue(Set set, String value) { - // TODO: Implement this method + if(set.contains(value)){ + return true; + } return false; } @@ -50,6 +79,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 +94,7 @@ public static void removeValue(Set set, String value) { public static int getUniqueCount(Set set) { // TODO: Implement this method - - return 0; + return set.size(); } /* @@ -79,7 +108,7 @@ public static int getUniqueCount(Set set) { public static Set getUniqueValues(List numbers) { // TODO: Implement this method + return new HashSet<>(numbers); - return null; } }