From 870f42876d7400e9df3f85b16251bc83613b68ff Mon Sep 17 00:00:00 2001 From: Kiyum-06 Date: Sun, 15 Jun 2025 11:53:30 -0700 Subject: [PATCH 1/3] Completed the CommonElementsFinder. --- src/CommonElementsFinder.java | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) 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); From e8742e86427f0fceb1b5a7e3d0f296f1b17f3a85 Mon Sep 17 00:00:00 2001 From: Kiyum-06 Date: Sun, 15 Jun 2025 12:08:20 -0700 Subject: [PATCH 2/3] Completed the DuplicateRemover. --- src/DuplicateRemover.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) 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); } From a7dc8aebd0cb06ec9bf9bf898b37c8819f564e1c Mon Sep 17 00:00:00 2001 From: Kiyum-06 Date: Sun, 15 Jun 2025 12:31:14 -0700 Subject: [PATCH 3/3] Completed UniqueCharacterChecker. --- src/UniqueCharacterChecker.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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) {