Skip to content

Commit 2ec4a1f

Browse files
authored
Enhance docs, add more tests in NumberAppearingOddTimes (#5857)
1 parent 6fc53ec commit 2ec4a1f

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Find the Number Appearing Odd Times in an array
4+
* This class provides a method to find the element that appears an
5+
* odd number of times in an array. All other elements in the array
6+
* must appear an even number of times for the logic to work.
7+
*
8+
* The solution uses the XOR operation, which has the following properties:
9+
* - a ^ a = 0 (XOR-ing the same numbers cancels them out)
10+
* - a ^ 0 = a
11+
* - XOR is commutative and associative.
12+
*
13+
* Time Complexity: O(n), where n is the size of the array.
14+
* Space Complexity: O(1), as no extra space is used.
15+
*
16+
* Usage Example:
17+
* int result = NumberAppearingOddTimes.findOddOccurrence(new int[]{1, 2, 1, 2, 3});
18+
* // result will be 3
19+
*
520
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
621
*/
722

823
public final class NumberAppearingOddTimes {
924
private NumberAppearingOddTimes() {
1025
}
26+
27+
/**
28+
* Finds the element in the array that appears an odd number of times.
29+
*
30+
* @param arr the input array containing integers, where all elements
31+
* except one appear an even number of times.
32+
* @return the integer that appears an odd number of times.
33+
*/
1134
public static int findOddOccurrence(int[] arr) {
1235
int result = 0;
13-
14-
// XOR all elements in the array
1536
for (int num : arr) {
1637
result ^= num;
1738
}
18-
1939
return result;
2040
}
2141
}

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

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,48 @@
22

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

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
69

710
class NumberAppearingOddTimesTest {
811

9-
@Test
10-
void testFindOddOccurrence() {
11-
int[] arr1 = {5, 6, 7, 8};
12-
assertEquals(12, NumberAppearingOddTimes.findOddOccurrence(arr1));
12+
/**
13+
* Parameterized test for findOddOccurrence method. Tests multiple
14+
* input arrays and their expected results.
15+
*/
16+
@ParameterizedTest
17+
@MethodSource("provideTestCases")
18+
void testFindOddOccurrence(int[] input, int expected) {
19+
assertEquals(expected, NumberAppearingOddTimes.findOddOccurrence(input));
20+
}
21+
22+
/**
23+
* Provides test cases for the parameterized test.
24+
* Each test case consists of an input array and the expected result.
25+
*/
26+
private static Stream<Arguments> provideTestCases() {
27+
return Stream.of(
28+
// Single element appearing odd times (basic case)
29+
Arguments.of(new int[] {5, 6, 7, 8, 6, 7, 5}, 8),
30+
31+
// More complex case with multiple pairs
32+
Arguments.of(new int[] {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2}, 5),
33+
34+
// Case with only one element appearing once
35+
Arguments.of(new int[] {10, 10, 20, 20, 30}, 30),
36+
37+
// Negative numbers with an odd occurrence
38+
Arguments.of(new int[] {-5, -5, -3, -3, -7, -7, -7}, -7),
1339

14-
int[] arr2 = {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
15-
assertEquals(5, NumberAppearingOddTimes.findOddOccurrence(arr2));
40+
// All elements cancel out to 0 (even occurrences of all elements)
41+
Arguments.of(new int[] {1, 2, 1, 2}, 0),
1642

17-
int[] arr3 = {10, 10, 20, 20, 30};
18-
assertEquals(30, NumberAppearingOddTimes.findOddOccurrence(arr3));
43+
// Array with a single element (trivial case)
44+
Arguments.of(new int[] {42}, 42),
1945

20-
int[] arr4 = {-5, -5, -3, -3, -7, -7, -7};
21-
assertEquals(-7, NumberAppearingOddTimes.findOddOccurrence(arr4));
46+
// Large array with repeated patterns
47+
Arguments.of(new int[] {1, 1, 2, 2, 3, 3, 3, 4, 4}, 3));
2248
}
2349
}

0 commit comments

Comments
 (0)