From 083e6466d10008561352108d793ea84fbbc53209 Mon Sep 17 00:00:00 2001 From: Gulyapas Poonkawinsiri <67386394+joetlobb@users.noreply.github.com> Date: Mon, 9 Jun 2025 11:17:58 -0700 Subject: [PATCH 1/4] complete sortAndRemoveDuplicates method --- src/DuplicateRemover.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/DuplicateRemover.java b/src/DuplicateRemover.java index 8d70003..f1bcb48 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,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 dc6d33b035760c6378040c1e6e0363bbd1541d6f Mon Sep 17 00:00:00 2001 From: Gulyapas Poonkawinsiri <67386394+joetlobb@users.noreply.github.com> Date: Mon, 9 Jun 2025 11:25:08 -0700 Subject: [PATCH 2/4] complete hasUniqueCharacters method --- src/UniqueCharacterChecker.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index 554ffc4..7b49d90 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,11 @@ 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 charSet = new HashSet<>(); + for (char c: word.toCharArray()) { + charSet.add(c); + } + return charSet.size() == word.length(); } public static void main(String[] args) { From 50c0f0a06bf4eb402f3fc20723a88d2ba45f42b2 Mon Sep 17 00:00:00 2001 From: Gulyapas Poonkawinsiri <67386394+joetlobb@users.noreply.github.com> Date: Mon, 9 Jun 2025 11:32:41 -0700 Subject: [PATCH 3/4] complete findCommonElements method --- src/CommonElementsFinder.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/CommonElementsFinder.java b/src/CommonElementsFinder.java index fb88a4b..643204b 100644 --- a/src/CommonElementsFinder.java +++ b/src/CommonElementsFinder.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Set; /** @@ -14,8 +15,20 @@ 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 set1 = new HashSet<>(); + Set set2 = new HashSet<>(); + for (int i: array1) { + set1.add(i); + } + for (int i: array2) { + set2.add(i); + } + Set commonSet = new HashSet<>(); + for (int i: set1) { + if (set2.contains(i)) commonSet.add(i); + } + + return commonSet; } From a1ce2bcc3b4bd5f0d51655d1491012e7df679b3f Mon Sep 17 00:00:00 2001 From: Gulyapas Poonkawinsiri <67386394+joetlobb@users.noreply.github.com> Date: Mon, 9 Jun 2025 11:49:05 -0700 Subject: [PATCH 4/4] optimize findCommonElements method --- src/CommonElementsFinder.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/CommonElementsFinder.java b/src/CommonElementsFinder.java index 643204b..f8775bb 100644 --- a/src/CommonElementsFinder.java +++ b/src/CommonElementsFinder.java @@ -16,16 +16,12 @@ public class CommonElementsFinder { */ public static Set findCommonElements(int[] array1, int[] array2) { Set set1 = new HashSet<>(); - Set set2 = new HashSet<>(); for (int i: array1) { set1.add(i); } - for (int i: array2) { - set2.add(i); - } Set commonSet = new HashSet<>(); - for (int i: set1) { - if (set2.contains(i)) commonSet.add(i); + for (int i: array2) { + if (set1.contains(i)) commonSet.add(i); } return commonSet;