diff --git a/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java b/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java new file mode 100644 index 00000000..cd3df5ae --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java @@ -0,0 +1,17 @@ +package com.thealgorithms.bitmanipulation; + +/** + * Find Non Repeating Number + * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) + */ + +public class NonRepeatingNumberFinder { + + public static int findNonRepeatingNumber(int[] arr) { + int result = 0; + for (int num : arr) { + result ^= num; + } + return result; + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java b/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java new file mode 100644 index 00000000..330b57dc --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java @@ -0,0 +1,23 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +/** + * Test case for Non Repeating Number Finder + * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) + */ + +class NonRepeatingNumberFinderTest { + + @Test + void testNonRepeatingNumberFinder() { + int arr[] = {1, 2, 1, 2, 6}; + assertEquals(6, NonRepeatingNumberFinder.findNonRepeatingNumber(arr)); + int arr1[] = {1, 2, 1, 2}; + assertEquals(0, NonRepeatingNumberFinder.findNonRepeatingNumber(arr1)); + int arr2[] = {12}; + assertEquals(12, NonRepeatingNumberFinder.findNonRepeatingNumber(arr2)); + } +} \ No newline at end of file