From bf1abab4935e29c3a7bde86d83e4744ff5eb8bc5 Mon Sep 17 00:00:00 2001 From: Johncarlo Perez Date: Mon, 9 Jun 2025 11:17:45 -0700 Subject: [PATCH 1/8] implemented logic for sortAndRemoveDuplicates --- src/DuplicateRemover.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/DuplicateRemover.java b/src/DuplicateRemover.java index 8d70003..b27c239 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 @@ -15,7 +18,15 @@ public class DuplicateRemover { */ 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 5d3ffcd2c293e66809a73888be30745109407bea Mon Sep 17 00:00:00 2001 From: Johncarlo Perez Date: Mon, 9 Jun 2025 18:57:24 -0700 Subject: [PATCH 2/8] finished implementing method 1 logic --- src/UniqueCharacterChecker.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index 554ffc4..a62b632 100644 --- a/src/UniqueCharacterChecker.java +++ b/src/UniqueCharacterChecker.java @@ -1,3 +1,7 @@ +import java.util.HashSet; +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,7 +18,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 seenChar = new HashSet<>(); + for(char c : word.toCharArray()) { + if(seenChar.contains(c)) { + return false; + } + seenChar.add(c); + } + + return true; } public static void main(String[] args) { From 0f1a0406c9cdd5971e527f102c66297291487b93 Mon Sep 17 00:00:00 2001 From: Johncarlo Perez Date: Mon, 9 Jun 2025 19:22:32 -0700 Subject: [PATCH 3/8] finished implementing logic for second method --- src/CommonElementsFinder.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/CommonElementsFinder.java b/src/CommonElementsFinder.java index fb88a4b..12256df 100644 --- a/src/CommonElementsFinder.java +++ b/src/CommonElementsFinder.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Set; /** @@ -14,8 +15,21 @@ 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 arr1Elements = new HashSet<>(); + + for(int i = 0; i < array1.length; i++) { + arr1Elements.add(array1[i]); + } + + Set commonElements = new HashSet<>(); + + for(int num : array2) { + if(arr1Elements.contains(num)) { + commonElements.add(num); + } + } + return commonElements; } From e54ebf05c1c8629705bbd60de03cdc38d7f4744e Mon Sep 17 00:00:00 2001 From: Johncarlo Perez Date: Mon, 9 Jun 2025 20:01:31 -0700 Subject: [PATCH 4/8] added junit testing for commonelements --- src/JunitTesting.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/JunitTesting.java diff --git a/src/JunitTesting.java b/src/JunitTesting.java new file mode 100644 index 0000000..699b865 --- /dev/null +++ b/src/JunitTesting.java @@ -0,0 +1,35 @@ +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import org.junit.Test; + +public class JunitTesting { + @org.junit.jupiter.api.Test + void testForCommonElms() { + Set expected = new HashSet<>(Arrays.asList(4,5,6)); + assertEquals(expected, CommonElementsFinder.findCommonElements(new int[]{4,5,6}, new int[]{4,5,6})); + } + + @org.junit.jupiter.api.Test + void testForCommonElms_OneElms() { + Set expected = new HashSet<>(Arrays.asList(3)); + assertEquals(expected, CommonElementsFinder.findCommonElements(new int[]{1,2,3}, new int[]{3,4,5})); + } + + @org.junit.jupiter.api.Test + void testForCommonElms_TwoElms() { + Set expected = new HashSet<>(Arrays.asList(2,3)); + assertEquals(expected, CommonElementsFinder.findCommonElements(new int[]{1,2,3}, new int[]{2,3,4,5})); + } + + @org.junit.jupiter.api.Test + void testForCommonElms_NoCommon() { + Set expected = new HashSet<>(Arrays.asList()); + assertEquals(expected, CommonElementsFinder.findCommonElements(new int[]{1,2,3}, new int[]{4,5,6})); + } +} + + From 665d0ff4c299dea7283d4d5b99b7a8d18ef699ce Mon Sep 17 00:00:00 2001 From: Johncarlo Perez Date: Mon, 9 Jun 2025 20:14:25 -0700 Subject: [PATCH 5/8] added junit test for UniqueCharChecker --- src/JunitTesting.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/JunitTesting.java b/src/JunitTesting.java index 699b865..992192b 100644 --- a/src/JunitTesting.java +++ b/src/JunitTesting.java @@ -30,6 +30,16 @@ void testForCommonElms_NoCommon() { Set expected = new HashSet<>(Arrays.asList()); assertEquals(expected, CommonElementsFinder.findCommonElements(new int[]{1,2,3}, new int[]{4,5,6})); } + + @org.junit.jupiter.api.Test + void testForUniqueChar() { + assertTrue(UniqueCharacterChecker.hasUniqueCharacters("bye")); + } + + @org.junit.jupiter.api.Test + void testForNonUniqeChar() { + assertFalse(UniqueCharacterChecker.hasUniqueCharacters("Hello")); + } } From 47e0eee22d3ef1e7339b792e83a3bf76d53321ad Mon Sep 17 00:00:00 2001 From: Johncarlo Perez Date: Mon, 9 Jun 2025 20:49:04 -0700 Subject: [PATCH 6/8] added more junit testing --- src/JunitTesting.java | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/JunitTesting.java b/src/JunitTesting.java index 992192b..be751d0 100644 --- a/src/JunitTesting.java +++ b/src/JunitTesting.java @@ -1,7 +1,10 @@ import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; import org.junit.Test; @@ -40,6 +43,44 @@ void testForUniqueChar() { void testForNonUniqeChar() { assertFalse(UniqueCharacterChecker.hasUniqueCharacters("Hello")); } + + @org.junit.jupiter.api.Test + void testForUniqueChar_null() { + assertTrue(UniqueCharacterChecker.hasUniqueCharacters("")); + } + + @org.junit.jupiter.api.Test + void testForDuplicateRemover() { + List expected = Arrays.asList("bye", "hello"); + assertEquals(expected, DuplicateRemover.sortAndRemoveDuplicates(new String[]{"bye", "hello", "bye"})); + } + + @org.junit.jupiter.api.Test + void testForDuplicateRemover_Null() { + List expected = Arrays.asList(); + assertEquals(expected, DuplicateRemover.sortAndRemoveDuplicates(new String[]{})); + } + + @org.junit.jupiter.api.Test + void testForDuplicateRemover_allDup() { + List expected = Arrays.asList("bye"); + assertEquals(expected, DuplicateRemover.sortAndRemoveDuplicates(new String[]{"bye", "bye", "bye", "bye"})); + } + @org.junit.jupiter.api.Test + void testForDuplicateRemover_case() { + List expected = Arrays.asList("Bye", "hello"); + assertEquals(expected, DuplicateRemover.sortAndRemoveDuplicates(new String[]{"Bye", "hello"})); + } + + @org.junit.jupiter.api.Test + void testForDuplicateRemover_caseAllSame() { + List expected = Arrays.asList("Hello", "hello"); + assertEquals(expected, DuplicateRemover.sortAndRemoveDuplicates(new String[]{"Hello", "Hello", "Hello", "hello"})); + } } + + + + From 5274c61f3561a3f389dfe4eb26703e20d0cfa965 Mon Sep 17 00:00:00 2001 From: Johncarlo Perez Date: Mon, 9 Jun 2025 20:58:27 -0700 Subject: [PATCH 7/8] adjusted logic for uniqueChar method --- src/UniqueCharacterChecker.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index a62b632..c57395e 100644 --- a/src/UniqueCharacterChecker.java +++ b/src/UniqueCharacterChecker.java @@ -19,11 +19,11 @@ public static boolean hasUniqueCharacters(String 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 Set seenChar = new HashSet<>(); - for(char c : word.toCharArray()) { + for(int i = 0; i < word.length(); i++) { + char c = word.charAt(i); if(seenChar.contains(c)) { return false; } - seenChar.add(c); } return true; From 88c3ad0c8e182458ad7e79cb27ee01371f85d3e7 Mon Sep 17 00:00:00 2001 From: Johncarlo Perez Date: Mon, 9 Jun 2025 21:01:44 -0700 Subject: [PATCH 8/8] added a piece of logic for uniqueChar method --- src/UniqueCharacterChecker.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index c57395e..8bab9ba 100644 --- a/src/UniqueCharacterChecker.java +++ b/src/UniqueCharacterChecker.java @@ -24,6 +24,7 @@ public static boolean hasUniqueCharacters(String word) { if(seenChar.contains(c)) { return false; } + seenChar.add(c); } return true;