Repository navigation
Add solution for "The Two Sneaky Numbers of Digitville" with unit tests. - #37
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a solution for LeetCode problem #3289 "The Two Sneaky Numbers of Digitville" with corresponding unit tests. The solution finds two numbers that appear twice in an array using a HashSet-based approach with O(n) time complexity.
Changes:
- Implemented
getSneakyNumbersmethod using HashSet to track seen numbers and identify duplicates - Added three test cases covering the provided problem examples
- Included complexity analysis documentation
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| TheTwoSneakyNumbersOfDigitville3289.java | Main solution implementation using HashSet to find duplicate numbers with early termination optimization |
| TheTwoSneakyNumbersOfDigitville3289Test.java | Unit tests with three example test cases that sort results before comparison |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /** | ||
| * Finds the two numbers that appear twice in the array. | ||
| * <p> | ||
| * Time Complexity: O(n) where n is the length of the array. | ||
| * We iterate through the array once, and HashSet operations (add, contains) are O(1) on average. | ||
| * <p> | ||
| * Space Complexity: O(n) for the HashSet to store seen numbers. | ||
| * In the worst case, we store almost all numbers before finding the duplicates. | ||
| */ |
There was a problem hiding this comment.
The documentation style is inconsistent with the rest of the codebase. Other solution files in this repository use multi-line block comments with /* / format for complexity analysis, rather than Javadoc-style /* */ comments. Consider changing to the standard block comment format used in files like ContainsDuplicate217.java and TwoSum1.java for consistency.
| @Test | ||
| void testGetSneakyNumbers_Example1() { | ||
| int[] nums = {0, 1, 1, 0}; | ||
| int[] expected = {0, 1}; | ||
|
|
||
| int[] result = solution.getSneakyNumbers(nums); | ||
| Arrays.sort(result); | ||
|
|
||
| assertArrayEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSneakyNumbers_Example2() { | ||
| int[] nums = {0, 3, 2, 1, 3, 2}; | ||
| int[] expected = {2, 3}; | ||
|
|
||
| int[] result = solution.getSneakyNumbers(nums); | ||
| Arrays.sort(result); | ||
|
|
||
| assertArrayEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testGetSneakyNumbers_Example3() { | ||
| int[] nums = {7, 1, 5, 4, 3, 4, 6, 0, 9, 5, 8, 2}; | ||
| int[] expected = {4, 5}; | ||
|
|
||
| int[] result = solution.getSneakyNumbers(nums); | ||
| Arrays.sort(result); | ||
|
|
||
| assertArrayEquals(expected, result); | ||
| } |
There was a problem hiding this comment.
The test coverage could be improved with additional edge cases. Other test files in this repository (e.g., MergeSortedArray88Test.java) include more comprehensive test coverage beyond just the provided examples. Consider adding tests for edge cases such as: a minimal array where n=2 (e.g., [0, 0]), cases where duplicates appear consecutively, or cases where duplicates are at the start or end of the array.
No description provided.