Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/CommonElementsFinder.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.util.HashSet;
import java.util.Set;

/**
Expand All @@ -14,8 +15,26 @@ public class CommonElementsFinder {
* @return a Set<Integer> containing the integers that are present in both arrays
*/
public static Set<Integer> findCommonElements(int[] array1, int[] array2) {
// TODO
return null;
// Create a HashSet to store elements from the first array
HashSet<Integer> 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<Integer> 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;

}


Expand Down
23 changes: 21 additions & 2 deletions src/DuplicateRemover.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,8 +17,24 @@ public class DuplicateRemover {
* @return a sorted List<String> containing unique words from the input array
*/
public static List<String> sortAndRemoveDuplicates(String[] words) {
// TODO
return null;
//use tree set to store unique words by creatung a new TreeSet
Set<String> 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<String> 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) {
Expand Down
19 changes: 18 additions & 1 deletion src/UniqueCharacterChecker.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<Character> 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) {
Expand All @@ -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));


}
}