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 <alexanderklmn@gmail.com>
This commit is contained in:
Tuhinm2002
2024-10-10 13:02:02 +05:30
committed by GitHub
parent 6535d4755d
commit d4fff30eaa
2 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package com.thealgorithms.bitmanipulation;
/**
* Utility class to find the single non-duplicate element from an array
* where all other elements appear twice.
* <p>
* The algorithm runs in O(n) time complexity and O(1) space complexity
* using bitwise XOR.
* </p>
*
* @author <a href="http://github.com/tuhinm2002">Tuhin M</a>
*/
public final class SingleElement {
/**
* Private constructor to prevent instantiation of this utility class.
* Throws an UnsupportedOperationException if attempted.
*/
private SingleElement() {
throw new UnsupportedOperationException("Utility Class");
}
/**
* Finds the single non-duplicate element in an array where every other
* element appears exactly twice. Uses bitwise XOR to achieve O(n) time
* complexity and O(1) space complexity.
*
* @param arr the input array containing integers where every element
* except one appears exactly twice
* @return the single non-duplicate element
*/
public static int findSingleElement(int[] arr) {
int ele = 0;
for (int i = 0; i < arr.length; i++) {
ele ^= arr[i];
}
return ele;
}
}

View File

@ -0,0 +1,33 @@
package com.thealgorithms.bitmanipulation;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public final class SingleElementTest {
/**
* Parameterized test to find the single non-duplicate element
* in the given arrays.
*
* @param arr the input array where every element appears twice except one
* @param expected the expected single element result
*/
@ParameterizedTest
@MethodSource("provideTestCases")
void testFindSingleElement(int[] arr, int expected) {
assertEquals(expected, SingleElement.findSingleElement(arr));
}
/**
* Provides test cases for the parameterized test.
*
* @return Stream of arguments consisting of arrays and expected results
*/
private static Stream<Arguments> provideTestCases() {
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));
}
}