diff --git a/src/CommonElementsFinder.java b/src/CommonElementsFinder.java index fb88a4b..0c66265 100644 --- a/src/CommonElementsFinder.java +++ b/src/CommonElementsFinder.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Set; /** @@ -11,17 +12,29 @@ public class CommonElementsFinder { * * @param array1 the first array of integers * @param array2 the second array of integers - * @return a Set containing the integers that are present in both arrays + * @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 common = new HashSet<>(); + + for (int num : array1) { + set1.add(num); + } + for (int num : array2) { + if (set1.contains(num)) { + common.add(num); + } + } + + return common; + } public static void main(String[] args) { - int[] array1 = {1, 2, 3, 4, 5}; - int[] array2 = {4, 5, 6, 7, 8}; + int[] array1 = { 1, 2, 3, 4, 5 }; + int[] array2 = { 4, 5, 6, 7, 8 }; Set common = findCommonElements(array1, array2); System.out.println("Common elements: " + common); diff --git a/src/DuplicateRemover.java b/src/DuplicateRemover.java index 8d70003..0b93825 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 @@ -7,19 +10,25 @@ public class DuplicateRemover { /** - * Removes duplicate words from the input array, sorts them in lexicographic order, + * Removes duplicate words from the input array, sorts them in lexicographic + * order, * and returns a list of unique words. * * @param words an array of strings that may contain duplicate words * @return a sorted List containing unique words from the input array */ public static List sortAndRemoveDuplicates(String[] words) { - // TODO - return null; + Set sortedSet = new TreeSet<>(); + + for (String word : words) { + sortedSet.add(word); + } + + return new ArrayList<>(sortedSet); } public static void main(String[] args) { - String[] words = {"yes", "no", "maybe", "yes", "yes"}; + String[] words = { "yes", "no", "maybe", "yes", "yes" }; List uniqueWords = sortAndRemoveDuplicates(words); System.out.println(uniqueWords); } diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index 554ffc4..3e69e77 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. @@ -12,9 +15,20 @@ public class UniqueCharacterChecker { */ 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 + // 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) {