diff --git a/src/CommonElementsFinder.java b/src/CommonElementsFinder.java index fb88a4b..9815d1c 100644 --- a/src/CommonElementsFinder.java +++ b/src/CommonElementsFinder.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Set; /** @@ -14,9 +15,19 @@ 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 numsArr1 = new HashSet<>(); + for (int i = 0; i < array1.length; i++) { + numsArr1.add(array1[i]); + } + Set commonNums = new HashSet<>(); + for (int j = 0; j < array2.length; j++) { + if(numsArr1.contains(array2[j])) { + commonNums.add(array2[j]); + } + } + return commonNums; + } + public static void main(String[] args) { diff --git a/src/DuplicateRemover.java b/src/DuplicateRemover.java index 8d70003..9ef82e9 100644 --- a/src/DuplicateRemover.java +++ b/src/DuplicateRemover.java @@ -1,4 +1,7 @@ +import java.util.ArrayList; import java.util.List; +import java.util.Set; +import java.util.TreeSet; /** * The DuplicateRemover class provides a method to remove duplicate words @@ -14,10 +17,24 @@ public class DuplicateRemover { * @return a sorted List containing unique words from the input array */ public static List sortAndRemoveDuplicates(String[] words) { - // TODO - return null; + + Set unique = new TreeSet<>(); + + for (String word : words) { + unique.add(word); + } + + List wordList = new ArrayList(); + + for (String word : unique) { + wordList.add(word); + } + + return wordList; + } + public static void main(String[] args) { String[] words = {"yes", "no", "maybe", "yes", "yes"}; List uniqueWords = sortAndRemoveDuplicates(words); diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index 554ffc4..1fbdb0d 100644 --- a/src/UniqueCharacterChecker.java +++ b/src/UniqueCharacterChecker.java @@ -1,3 +1,5 @@ +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. @@ -11,11 +13,20 @@ public class UniqueCharacterChecker { * @return true if all characters in the word are unique; false otherwise */ 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 characters = new HashSet<>(); + + for (int i = 0; i < word.length(); i++) { + char currChar = word.charAt(i); + if (characters.contains(currChar)) { + return false; + } + characters.add(currChar); + } + + return true; + } + public static void main(String[] args) { String word1 = "hello";