diff --git a/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java b/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java index ce4e1d88da6e..bd4868d4dbd5 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java @@ -1,21 +1,41 @@ package com.thealgorithms.bitmanipulation; /** - * Find the Number Appearing Odd Times in an array + * This class provides a method to find the element that appears an + * odd number of times in an array. All other elements in the array + * must appear an even number of times for the logic to work. + * + * The solution uses the XOR operation, which has the following properties: + * - a ^ a = 0 (XOR-ing the same numbers cancels them out) + * - a ^ 0 = a + * - XOR is commutative and associative. + * + * Time Complexity: O(n), where n is the size of the array. + * Space Complexity: O(1), as no extra space is used. + * + * Usage Example: + * int result = NumberAppearingOddTimes.findOddOccurrence(new int[]{1, 2, 1, 2, 3}); + * // result will be 3 + * * @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999) */ public final class NumberAppearingOddTimes { private NumberAppearingOddTimes() { } + + /** + * Finds the element in the array that appears an odd number of times. + * + * @param arr the input array containing integers, where all elements + * except one appear an even number of times. + * @return the integer that appears an odd number of times. + */ public static int findOddOccurrence(int[] arr) { int result = 0; - - // XOR all elements in the array for (int num : arr) { result ^= num; } - return result; } } diff --git a/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java b/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java index d10b0f67b806..83d458319f3b 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java @@ -2,22 +2,48 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; class NumberAppearingOddTimesTest { - @Test - void testFindOddOccurrence() { - int[] arr1 = {5, 6, 7, 8}; - assertEquals(12, NumberAppearingOddTimes.findOddOccurrence(arr1)); + /** + * Parameterized test for findOddOccurrence method. Tests multiple + * input arrays and their expected results. + */ + @ParameterizedTest + @MethodSource("provideTestCases") + void testFindOddOccurrence(int[] input, int expected) { + assertEquals(expected, NumberAppearingOddTimes.findOddOccurrence(input)); + } + + /** + * Provides test cases for the parameterized test. + * Each test case consists of an input array and the expected result. + */ + private static Stream provideTestCases() { + return Stream.of( + // Single element appearing odd times (basic case) + Arguments.of(new int[] {5, 6, 7, 8, 6, 7, 5}, 8), + + // More complex case with multiple pairs + Arguments.of(new int[] {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2}, 5), + + // Case with only one element appearing once + Arguments.of(new int[] {10, 10, 20, 20, 30}, 30), + + // Negative numbers with an odd occurrence + Arguments.of(new int[] {-5, -5, -3, -3, -7, -7, -7}, -7), - int[] arr2 = {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2}; - assertEquals(5, NumberAppearingOddTimes.findOddOccurrence(arr2)); + // All elements cancel out to 0 (even occurrences of all elements) + Arguments.of(new int[] {1, 2, 1, 2}, 0), - int[] arr3 = {10, 10, 20, 20, 30}; - assertEquals(30, NumberAppearingOddTimes.findOddOccurrence(arr3)); + // Array with a single element (trivial case) + Arguments.of(new int[] {42}, 42), - int[] arr4 = {-5, -5, -3, -3, -7, -7, -7}; - assertEquals(-7, NumberAppearingOddTimes.findOddOccurrence(arr4)); + // Large array with repeated patterns + Arguments.of(new int[] {1, 1, 2, 2, 3, 3, 3, 4, 4}, 3)); } }