From 9abb15a32d02c74e78821bf82255648fc8a16137 Mon Sep 17 00:00:00 2001 From: konradkelly Date: Mon, 9 Jun 2025 11:18:01 -0700 Subject: [PATCH 1/5] implemented DuplicateRemover --- src/DuplicateRemover.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/DuplicateRemover.java b/src/DuplicateRemover.java index 8d70003..cea29c2 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,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) { String[] words = {"yes", "no", "maybe", "yes", "yes"}; List uniqueWords = sortAndRemoveDuplicates(words); From 4b0e600af6e05d680bd52d6fa52d26788bd4529b Mon Sep 17 00:00:00 2001 From: konradkelly Date: Tue, 10 Jun 2025 22:12:08 -0700 Subject: [PATCH 2/5] added sortAndRemoveDuplicates method --- src/CommonElementsFinder.java | 14 ++++++++++++-- src/DuplicateRemover.java | 6 ++++++ src/UniqueCharacterChecker.java | 12 +++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/CommonElementsFinder.java b/src/CommonElementsFinder.java index fb88a4b..f8cb4e2 100644 --- a/src/CommonElementsFinder.java +++ b/src/CommonElementsFinder.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Set; /** @@ -14,8 +15,17 @@ 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; } diff --git a/src/DuplicateRemover.java b/src/DuplicateRemover.java index cea29c2..9ef82e9 100644 --- a/src/DuplicateRemover.java +++ b/src/DuplicateRemover.java @@ -17,15 +17,21 @@ public class DuplicateRemover { * @return a sorted List containing unique words from the input array */ public static List sortAndRemoveDuplicates(String[] words) { + Set unique = new TreeSet<>(); + for (String word : words) { unique.add(word); } + List wordList = new ArrayList(); + for (String word : unique) { wordList.add(word); } + return wordList; + } diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index 554ffc4..f8f7eda 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. @@ -14,7 +16,15 @@ 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) { From 1abdfa72478e4cef55b64440cc50b687ce7461ab Mon Sep 17 00:00:00 2001 From: konradkelly Date: Tue, 10 Jun 2025 22:17:49 -0700 Subject: [PATCH 3/5] added hasUniqueCharacters method --- src/UniqueCharacterChecker.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index f8f7eda..80666c0 100644 --- a/src/UniqueCharacterChecker.java +++ b/src/UniqueCharacterChecker.java @@ -13,10 +13,9 @@ 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 + Set characters = new HashSet<>(); + for (int i = 0; i < word.length(); i++) { char currChar = word.charAt(i); if (characters.contains(currChar)) { @@ -24,7 +23,9 @@ public static boolean hasUniqueCharacters(String word) { } characters.add(currChar); } + return true; + } } public static void main(String[] args) { From 5e4092042062b59393ded813cea7c1147a5e2d41 Mon Sep 17 00:00:00 2001 From: konradkelly Date: Tue, 10 Jun 2025 22:20:56 -0700 Subject: [PATCH 4/5] added findCommonElements method --- src/CommonElementsFinder.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CommonElementsFinder.java b/src/CommonElementsFinder.java index f8cb4e2..860bbd0 100644 --- a/src/CommonElementsFinder.java +++ b/src/CommonElementsFinder.java @@ -27,6 +27,7 @@ public static Set findCommonElements(int[] array1, int[] array2) { } return commonNums; } + } public static void main(String[] args) { From 8d81635354256d48b9915361364f23c25fac72aa Mon Sep 17 00:00:00 2001 From: konradkelly Date: Tue, 10 Jun 2025 22:49:37 -0700 Subject: [PATCH 5/5] removed extra curly braces --- src/CommonElementsFinder.java | 4 ++-- src/UniqueCharacterChecker.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CommonElementsFinder.java b/src/CommonElementsFinder.java index 860bbd0..9815d1c 100644 --- a/src/CommonElementsFinder.java +++ b/src/CommonElementsFinder.java @@ -26,8 +26,8 @@ public static Set findCommonElements(int[] array1, int[] array2) { } } return commonNums; - } - } + } + public static void main(String[] args) { diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index 80666c0..1fbdb0d 100644 --- a/src/UniqueCharacterChecker.java +++ b/src/UniqueCharacterChecker.java @@ -26,7 +26,7 @@ public static boolean hasUniqueCharacters(String word) { return true; } - } + public static void main(String[] args) { String word1 = "hello";