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
25 changes: 19 additions & 6 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 @@ -11,17 +12,29 @@ public class CommonElementsFinder {
*
* @param array1 the first array of integers
* @param array2 the second array of integers
* @return a Set<Integer> containing the integers that are present in both arrays
* @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;
}
Set<Integer> set1 = new HashSet<>();
Set<Integer> common = new HashSet<>();

for (int num : array1) {
set1.add(num);
}

for (int num : array2) {
if (set1.contains(num)) {
common.add(num);
}
}

return common;
}

public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = {4, 5, 6, 7, 8};
int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 4, 5, 6, 7, 8 };

Set<Integer> common = findCommonElements(array1, array2);
System.out.println("Common elements: " + common);
Expand Down
17 changes: 13 additions & 4 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 @@ -7,19 +10,25 @@
public class DuplicateRemover {

/**
* Removes duplicate words from the input array, sorts them in lexicographic order,
* Removes duplicate words from the input array, sorts them in lexicographic
* order,
* and returns a list of unique words.
*
* @param words an array of strings that may contain duplicate words
* @return a sorted List<String> containing unique words from the input array
*/
public static List<String> sortAndRemoveDuplicates(String[] words) {
// TODO
return null;
Set<String> sortedSet = new TreeSet<>();

for (String word : words) {
sortedSet.add(word);
}

return new ArrayList<>(sortedSet);
}

public static void main(String[] args) {
String[] words = {"yes", "no", "maybe", "yes", "yes"};
String[] words = { "yes", "no", "maybe", "yes", "yes" };
List<String> uniqueWords = sortAndRemoveDuplicates(words);
System.out.println(uniqueWords);
}
Expand Down
18 changes: 16 additions & 2 deletions 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 @@ -12,9 +15,20 @@ public class UniqueCharacterChecker {
*/
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
// 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> seen = new HashSet<>();

for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (seen.contains(c)) {
return false;
}
seen.add(c);
}

return true;
}

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