-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package aykrieger; | ||
|
||
import java.util.LinkedHashSet; | ||
|
||
public class FirstNonDuplicate { | ||
|
||
private LinkedHashSet<Integer> linkedSet = new LinkedHashSet<>(); | ||
|
||
public void add(int number) { | ||
if (!linkedSet.contains(number)) { | ||
linkedSet.add(number); | ||
} else { | ||
linkedSet.remove(number); | ||
} | ||
} | ||
|
||
public int firstNonDuplicate() { | ||
return linkedSet.isEmpty() ? -1 : linkedSet.iterator().next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package aykrieger; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Test class for {@link FirstNonDuplicate} | ||
*/ | ||
public class FirstNonDuplicateTest { | ||
|
||
@Test | ||
public void testFirstNonDuplicate() { | ||
FirstNonDuplicate firstNonDuplicate = new FirstNonDuplicate(); | ||
firstNonDuplicate.add(10); | ||
firstNonDuplicate.add(11); | ||
firstNonDuplicate.add(12); | ||
assertEquals(10, firstNonDuplicate.firstNonDuplicate()); | ||
firstNonDuplicate.add(10); | ||
assertEquals(11, firstNonDuplicate.firstNonDuplicate()); | ||
firstNonDuplicate.add(11); | ||
firstNonDuplicate.add(12); | ||
assertEquals(-1, firstNonDuplicate.firstNonDuplicate()); | ||
} | ||
|
||
|
||
} |