Add find non repeating number algorithm (#4328)

This commit is contained in:
Bama Charan Chhandogi 2023-08-24 23:06:12 +05:30 committed by GitHub
parent 52f365a915
commit b4f786369b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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));
}
}