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
18 changes: 16 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,21 @@ 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;

Set<Integer> arr1Elements = new HashSet<>();

for(int i = 0; i < array1.length; i++) {
arr1Elements.add(array1[i]);
}

Set<Integer> commonElements = new HashSet<>();

for(int num : array2) {
if(arr1Elements.contains(num)) {
commonElements.add(num);
}
}
return commonElements;
}


Expand Down
13 changes: 12 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,15 @@ 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
86 changes: 86 additions & 0 deletions src/JunitTesting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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;

public class JunitTesting {
@org.junit.jupiter.api.Test
void testForCommonElms() {
Set<Integer> 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<Integer> 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<Integer> 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<Integer> 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"));
}

@org.junit.jupiter.api.Test
void testForUniqueChar_null() {
assertTrue(UniqueCharacterChecker.hasUniqueCharacters(""));
}

@org.junit.jupiter.api.Test
void testForDuplicateRemover() {
List<String> expected = Arrays.asList("bye", "hello");
assertEquals(expected, DuplicateRemover.sortAndRemoveDuplicates(new String[]{"bye", "hello", "bye"}));
}

@org.junit.jupiter.api.Test
void testForDuplicateRemover_Null() {
List<String> expected = Arrays.asList();
assertEquals(expected, DuplicateRemover.sortAndRemoveDuplicates(new String[]{}));
}

@org.junit.jupiter.api.Test
void testForDuplicateRemover_allDup() {
List<String> expected = Arrays.asList("bye");
assertEquals(expected, DuplicateRemover.sortAndRemoveDuplicates(new String[]{"bye", "bye", "bye", "bye"}));
}
@org.junit.jupiter.api.Test
void testForDuplicateRemover_case() {
List<String> expected = Arrays.asList("Bye", "hello");
assertEquals(expected, DuplicateRemover.sortAndRemoveDuplicates(new String[]{"Bye", "hello"}));
}

@org.junit.jupiter.api.Test
void testForDuplicateRemover_caseAllSame() {
List<String> expected = Arrays.asList("Hello", "hello");
assertEquals(expected, DuplicateRemover.sortAndRemoveDuplicates(new String[]{"Hello", "Hello", "Hello", "hello"}));
}
}






15 changes: 14 additions & 1 deletion src/UniqueCharacterChecker.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,7 +18,16 @@ 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> seenChar = new HashSet<>();
for(int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if(seenChar.contains(c)) {
return false;
}
seenChar.add(c);
}

return true;
}

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