From 26f62e7f388f008d3a95dd9629811487d1bf20b8 Mon Sep 17 00:00:00 2001 From: Kelley Castillo Date: Mon, 9 Jun 2025 11:17:39 -0700 Subject: [PATCH 1/3] finished with duplicate --- src/DuplicateRemover.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/DuplicateRemover.java b/src/DuplicateRemover.java index 8d70003..bf9669a 100644 --- a/src/DuplicateRemover.java +++ b/src/DuplicateRemover.java @@ -1,4 +1,7 @@ import java.util.List; +import java.util.Set; +import java.util.ArrayList; +import java.util.TreeSet; /** * The DuplicateRemover class provides a method to remove duplicate words @@ -14,8 +17,15 @@ 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) { From 2410ea5474a0c532809a00de007aa405b35cc66f Mon Sep 17 00:00:00 2001 From: Kelley Castillo Date: Mon, 9 Jun 2025 12:25:09 -0700 Subject: [PATCH 2/3] finished with unique --- src/UniqueCharacterChecker.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index 554ffc4..e786ed8 100644 --- a/src/UniqueCharacterChecker.java +++ b/src/UniqueCharacterChecker.java @@ -1,3 +1,9 @@ +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + /** * The UniqueCharacterChecker class provides a method to check if all characters * in a given word are unique. @@ -14,8 +20,17 @@ 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 unique = new HashSet<>(); + for (int i = 0; i < word.length(); i++) { + char c = word.charAt(i); + if (unique.contains(c)) { + return false; + } + unique.add(c); } + return true; +} public static void main(String[] args) { String word1 = "hello"; From 4a550c34ad4e472a8d15c0beb59745925cca8b09 Mon Sep 17 00:00:00 2001 From: Kelley Castillo Date: Mon, 9 Jun 2025 12:38:28 -0700 Subject: [PATCH 3/3] finished --- src/CommonElementsFinder.java | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/CommonElementsFinder.java b/src/CommonElementsFinder.java index fb88a4b..fddf6d3 100644 --- a/src/CommonElementsFinder.java +++ b/src/CommonElementsFinder.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Set; /** @@ -14,11 +15,29 @@ 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; + // List common = new ArrayList<>(); + // for (int i : array1) { + // for (int y : array2) { + // if (i == y && !common.contains(i)) { + // common.add(i); + // } + // } + // } + // return common; + // } + Set i = new HashSet<>(); + Set common = new HashSet<>(); + for (int num : array1) { + i.add(num); + } + for (int num : array2) { + if (i.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};