Skip to content

Commit d4fff30

Browse files
Tuhinm2002alxkm
andauthored
feat : new bit manipulation algo Single element in an array (#5689)
* feat : new algo uniquesubseqcount * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCountTest.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCountTest.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCountTest.java * Update UniqueSubsequencesCount.java * Update UniqueSubsequencesCountTest.java * Update UniqueSubsequencesCountTest.java * Update UniqueSubsequencesCountTest.java * Update UniqueSubsequencesCount.java * feat : new bitmanipulation algo --------- Co-authored-by: Alex Klymenko <[email protected]>
1 parent 6535d47 commit d4fff30

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Utility class to find the single non-duplicate element from an array
5+
* where all other elements appear twice.
6+
* <p>
7+
* The algorithm runs in O(n) time complexity and O(1) space complexity
8+
* using bitwise XOR.
9+
* </p>
10+
*
11+
* @author <a href="http://github.com/tuhinm2002">Tuhin M</a>
12+
*/
13+
public final class SingleElement {
14+
15+
/**
16+
* Private constructor to prevent instantiation of this utility class.
17+
* Throws an UnsupportedOperationException if attempted.
18+
*/
19+
private SingleElement() {
20+
throw new UnsupportedOperationException("Utility Class");
21+
}
22+
23+
/**
24+
* Finds the single non-duplicate element in an array where every other
25+
* element appears exactly twice. Uses bitwise XOR to achieve O(n) time
26+
* complexity and O(1) space complexity.
27+
*
28+
* @param arr the input array containing integers where every element
29+
* except one appears exactly twice
30+
* @return the single non-duplicate element
31+
*/
32+
public static int findSingleElement(int[] arr) {
33+
int ele = 0;
34+
for (int i = 0; i < arr.length; i++) {
35+
ele ^= arr[i];
36+
}
37+
return ele;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public final class SingleElementTest {
11+
12+
/**
13+
* Parameterized test to find the single non-duplicate element
14+
* in the given arrays.
15+
*
16+
* @param arr the input array where every element appears twice except one
17+
* @param expected the expected single element result
18+
*/
19+
@ParameterizedTest
20+
@MethodSource("provideTestCases")
21+
void testFindSingleElement(int[] arr, int expected) {
22+
assertEquals(expected, SingleElement.findSingleElement(arr));
23+
}
24+
25+
/**
26+
* Provides test cases for the parameterized test.
27+
*
28+
* @return Stream of arguments consisting of arrays and expected results
29+
*/
30+
private static Stream<Arguments> provideTestCases() {
31+
return Stream.of(Arguments.of(new int[] {1, 1, 2, 2, 4, 4, 3}, 3), Arguments.of(new int[] {1, 2, 2, 3, 3}, 1), Arguments.of(new int[] {10}, 10));
32+
}
33+
}

0 commit comments

Comments
 (0)