Add find non repeating number algorithm (#4328)
This commit is contained in:
parent
52f365a915
commit
b4f786369b
@ -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;
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user