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
22 changes: 21 additions & 1 deletion src/CommonElementsFinder.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.util.Set;
import java.util.TreeSet;

/**
* The CommonElementsFinder class provides a method for finding common elements
Expand All @@ -15,7 +16,26 @@ public class CommonElementsFinder {
*/
public static Set<Integer> findCommonElements(int[] array1, int[] array2) {
// TODO
return null;

Set<Integer> set1 = new TreeSet<>();
for (int num : array1) {
set1.add(num);
}


Set<Integer> set2 = new TreeSet<>();
for (int num : array2) {
set2.add(num);
}

Set<Integer> commonNumSet = new TreeSet<>();
for (int num : set1) {
if (set2.contains(num)) {
commonNumSet.add(num);
}
}

return commonNumSet;
}


Expand Down
15 changes: 14 additions & 1 deletion 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 @@ -15,7 +18,17 @@ public class DuplicateRemover {
*/
public static List<String> sortAndRemoveDuplicates(String[] words) {
// TODO
return null;
Set<String> unique = new TreeSet<>();
for (String word : words) {
unique.add(word);
}

List<String> wordList = new ArrayList<>();
for (String word : unique) {
wordList.add(word);
}

return wordList;
}

public static void main(String[] args) {
Expand Down
12 changes: 11 additions & 1 deletion src/UniqueCharacterChecker.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,7 +17,14 @@ 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<Character> charSet = new HashSet<>();

for (char c : word.toCharArray()) {
charSet.add(c);
}

return charSet.size() == word.length();
}

public static void main(String[] args) {
Expand Down