From 7ff1abbbfd86077e84e144ac315d266487d2c1e1 Mon Sep 17 00:00:00 2001 From: Bluenzo <80460870+Bluenzo@users.noreply.github.com> Date: Mon, 9 Jun 2025 11:18:02 -0700 Subject: [PATCH 1/3] Duplicate remover --- src/DuplicateRemover.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/DuplicateRemover.java b/src/DuplicateRemover.java index 8d70003..45bcd91 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,24 @@ public class DuplicateRemover { * @return a sorted List containing unique words from the input array */ public static List sortAndRemoveDuplicates(String[] words) { - // TODO - return null; + //use tree set to store unique words by creatung a new TreeSet + Set unique = new TreeSet<>(); + //doing a for loop to iterate through the array + for(String word : words){ + //adding each word to the set by using the add method + //if the word is already in the set, it will not be added again, meaning duplicates are removed + unique.add(word); + } + + //convert the set to a list and in order to return it + List wordList = new ArrayList<>(); + //using a for loop to iterate through the set + for(String word : unique){ + //adding each unique word to the list, which will be sorted automatically due to the nature of TreeSet + wordList.add(word); + } + + return wordList; } public static void main(String[] args) { From df9b182c74f10213c03e3b6de245f4886b754dc2 Mon Sep 17 00:00:00 2001 From: Bluenzo <80460870+Bluenzo@users.noreply.github.com> Date: Tue, 10 Jun 2025 13:01:40 -0700 Subject: [PATCH 2/3] Unique Character --- src/UniqueCharacterChecker.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/UniqueCharacterChecker.java b/src/UniqueCharacterChecker.java index 554ffc4..6b99b10 100644 --- a/src/UniqueCharacterChecker.java +++ b/src/UniqueCharacterChecker.java @@ -1,3 +1,5 @@ +import java.util.HashSet; + /** * The UniqueCharacterChecker class provides a method to check if all characters * in a given word are unique. @@ -12,9 +14,22 @@ public class UniqueCharacterChecker { */ public static boolean hasUniqueCharacters(String word) { // TODO: implement this! + // Create a HashSet to store characters we've seen + HashSet uniqueChar = new HashSet<>(); + // Iterate through each character in the word + for (int i = 0; i < word.length(); i++) { + char currentChar = word.charAt(i); + // Check if the character is already in the set + if (uniqueChar.contains(currentChar)) { + return false; // Duplicate found + } + // Add the character to the set + uniqueChar.add(currentChar); + } // 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; + + return true; // All characters are unique } public static void main(String[] args) { @@ -25,5 +40,7 @@ public static void main(String[] args) { System.out.println(word1 + " has unique characters: " + hasUniqueCharacters(word1)); System.out.println(word2 + " has unique characters: " + hasUniqueCharacters(word2)); System.out.println(word3 + " has unique characters: " + hasUniqueCharacters(word3)); + + } } From 23b8157d55c62850aed1a8190e68d8575de20c7f Mon Sep 17 00:00:00 2001 From: Bluenzo <80460870+Bluenzo@users.noreply.github.com> Date: Tue, 10 Jun 2025 13:04:37 -0700 Subject: [PATCH 3/3] Commmon elements --- src/CommonElementsFinder.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/CommonElementsFinder.java b/src/CommonElementsFinder.java index fb88a4b..3b1cf48 100644 --- a/src/CommonElementsFinder.java +++ b/src/CommonElementsFinder.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Set; /** @@ -14,8 +15,26 @@ 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; + // Create a HashSet to store elements from the first array + HashSet set1 = new HashSet<>(); + // Iterate through the first array and add each element to the set + for(int num1 : array1) { + set1.add(num1); + } + + // Create a HashSet to store common elements + HashSet commonElements = new HashSet<>(); + // Iterate through the second array + for(int num2 : array2){ + // Check if the current element is in the first set + if(set1.contains(num2)) { + // If it is, add it to the common elements set + commonElements.add(num2); + } + } + // Return the set of common elements + return commonElements; + }