diff --git a/src/CommonElementsFinder.java b/src/CommonElementsFinder.java index fb88a4b..3614eae 100644 --- a/src/CommonElementsFinder.java +++ b/src/CommonElementsFinder.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Set; /** @@ -14,8 +15,21 @@ 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 uniqueNum = new HashSet<>(); + Set returnUnique = new HashSet<>(); + + for (int num : array1){ + uniqueNum.add(num); + } + + for (int num : array2){ + if (uniqueNum.contains(num)){ + returnUnique.add(num); + } + } + + + return returnUnique; } diff --git a/src/DuplicateRemover.java b/src/DuplicateRemover.java index 8d70003..0dae3ce 100644 --- a/src/DuplicateRemover.java +++ b/src/DuplicateRemover.java @@ -1,4 +1,7 @@ import java.util.List; +import java.util.TreeSet; +import java.util.ArrayList; +import java.util.Set; /** * The DuplicateRemover class provides a method to remove duplicate words @@ -14,8 +17,18 @@ 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) { diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index 554ffc4..27f9116 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,22 @@ 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; + + /* + * Solution I'm thinking of is we collect all the unique characters from the word. + * If the length of the Set list is different than the length of the word, then + * the word does not have all unique characters. + * + * I know it says to use charAt but while I was making the solution 'charAt' never came + * up as a tool to use. + */ + + Set uniqueChar = new HashSet<>(); + for (char letter : word.toCharArray()){ + uniqueChar.add(letter); + } + + return word.toCharArray().length == uniqueChar.size(); } public static void main(String[] args) {