Skip to content

Commit b4f7863

Browse files
Add find non repeating number algorithm (TheAlgorithms#4328)
1 parent 52f365a commit b4f7863

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Find Non Repeating Number
5+
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
6+
*/
7+
8+
public class NonRepeatingNumberFinder {
9+
10+
public static int findNonRepeatingNumber(int[] arr) {
11+
int result = 0;
12+
for (int num : arr) {
13+
result ^= num;
14+
}
15+
return result;
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Test case for Non Repeating Number Finder
9+
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
10+
*/
11+
12+
class NonRepeatingNumberFinderTest {
13+
14+
@Test
15+
void testNonRepeatingNumberFinder() {
16+
int arr[] = {1, 2, 1, 2, 6};
17+
assertEquals(6, NonRepeatingNumberFinder.findNonRepeatingNumber(arr));
18+
int arr1[] = {1, 2, 1, 2};
19+
assertEquals(0, NonRepeatingNumberFinder.findNonRepeatingNumber(arr1));
20+
int arr2[] = {12};
21+
assertEquals(12, NonRepeatingNumberFinder.findNonRepeatingNumber(arr2));
22+
}
23+
}

0 commit comments

Comments
 (0)