Skip to content

Commit 788f4d8

Browse files
authored
Enhance docs, add more tests in NonRepeatingNumberFinder (#5843)
1 parent 82dee61 commit 788f4d8

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Find Non Repeating Number
4+
* A utility class to find the non-repeating number in an array where every other number repeats.
5+
* This class contains a method to identify the single unique number using bit manipulation.
6+
*
7+
* The solution leverages the properties of the XOR operation, which states that:
8+
* - x ^ x = 0 for any integer x (a number XORed with itself is zero)
9+
* - x ^ 0 = x for any integer x (a number XORed with zero is the number itself)
10+
*
11+
* Using these properties, we can find the non-repeating number in linear time with constant space.
12+
*
13+
* Example:
14+
* Given the input array [2, 3, 5, 2, 3], the output will be 5 since it does not repeat.
15+
*
516
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
617
*/
7-
818
public final class NonRepeatingNumberFinder {
919
private NonRepeatingNumberFinder() {
1020
}
1121

22+
/**
23+
* Finds the non-repeating number in the given array.
24+
*
25+
* @param arr an array of integers where every number except one appears twice
26+
* @return the integer that appears only once in the array or 0 if the array is empty
27+
*/
1228
public static int findNonRepeatingNumber(int[] arr) {
1329
int result = 0;
1430
for (int num : arr) {

src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java

+23-10
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,35 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
68

79
/**
810
* Test case for Non Repeating Number Finder
11+
* This test class validates the functionality of the
12+
* NonRepeatingNumberFinder by checking various scenarios.
13+
*
914
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
1015
*/
11-
1216
class NonRepeatingNumberFinderTest {
1317

14-
@Test
15-
void testNonRepeatingNumberFinder() {
16-
int[] arr = {1, 2, 1, 2, 6};
17-
assertEquals(6, NonRepeatingNumberFinder.findNonRepeatingNumber(arr));
18-
int[] arr1 = {1, 2, 1, 2};
19-
assertEquals(0, NonRepeatingNumberFinder.findNonRepeatingNumber(arr1));
20-
int[] arr2 = {12};
21-
assertEquals(12, NonRepeatingNumberFinder.findNonRepeatingNumber(arr2));
18+
@ParameterizedTest
19+
@MethodSource("testCases")
20+
void testNonRepeatingNumberFinder(int[] arr, int expected) {
21+
assertEquals(expected, NonRepeatingNumberFinder.findNonRepeatingNumber(arr));
22+
}
23+
24+
private static Arguments[] testCases() {
25+
return new Arguments[] {
26+
Arguments.of(new int[] {1, 2, 1, 2, 6}, 6), Arguments.of(new int[] {1, 2, 1, 2}, 0), // All numbers repeat
27+
Arguments.of(new int[] {12}, 12), // Single non-repeating number
28+
Arguments.of(new int[] {3, 5, 3, 4, 4}, 5), // More complex case
29+
Arguments.of(new int[] {7, 8, 7, 9, 8, 10, 10}, 9), // Non-repeating in the middle
30+
Arguments.of(new int[] {0, -1, 0, -1, 2}, 2), // Testing with negative numbers
31+
Arguments.of(new int[] {Integer.MAX_VALUE, 1, 1}, Integer.MAX_VALUE), // Edge case with max int
32+
Arguments.of(new int[] {2, 2, 3, 3, 4, 5, 4}, 5), // Mixed duplicates
33+
Arguments.of(new int[] {}, 0) // Edge case: empty array (should be handled as per design)
34+
};
2235
}
2336
}

0 commit comments

Comments
 (0)