diff --git a/src/CommonElementsFinder.java b/src/CommonElementsFinder.java index fb88a4b..6684a09 100644 --- a/src/CommonElementsFinder.java +++ b/src/CommonElementsFinder.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Set; /** @@ -14,8 +15,20 @@ public class CommonElementsFinder { * @return a Set containing the integers that are present in both arrays */ public static Set findCommonElements(int[] array1, int[] array2) { - // TODO - return null; + Set set1 = new HashSet<>(); + Set result = new HashSet<>(); + + for (int num : array1) { + set1.add(num); + } + + for (int num : array2) { + if (set1.contains(num)) { + result.add(num); + } + } + + return result; } diff --git a/src/DuplicateRemover.java b/src/DuplicateRemover.java index 8d70003..d05f300 100644 --- a/src/DuplicateRemover.java +++ b/src/DuplicateRemover.java @@ -1,4 +1,8 @@ +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Set; +import java.util.TreeSet; /** * The DuplicateRemover class provides a method to remove duplicate words @@ -14,8 +18,8 @@ public class DuplicateRemover { * @return a sorted List containing unique words from the input array */ public static List sortAndRemoveDuplicates(String[] words) { - // TODO - return null; + Set set = new TreeSet<>(Arrays.asList(words)); + return new ArrayList<>(set); } public static void main(String[] args) { diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index 554ffc4..5024afa 100644 --- a/src/UniqueCharacterChecker.java +++ b/src/UniqueCharacterChecker.java @@ -1,3 +1,6 @@ +import java.util.HashSet; +import java.util.Set; + /** * The UniqueCharacterChecker class provides a method to check if all characters * in a given word are unique. @@ -14,7 +17,18 @@ public static boolean hasUniqueCharacters(String word) { // TODO: implement this! // Requirement: This must run in O(n) time, where n is the number of characters in the word // Hint: Stuck? Consider looking up "charAt" and seeing how it can help you - return false; + + Set seen = new HashSet<>(); + + for (int i = 0; i < word.length(); i++) { + char c = word.charAt(i); + if (seen.contains(c)) { + return false; + } + seen.add(c); + } + + return true; } public static void main(String[] args) {