Skip to content

feat : new bit manipulation algo Single element in an array #5689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c7c3d8b
feat : new algo uniquesubseqcount
Tuhinm2002 Oct 5, 2024
d124680
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
d99c476
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 5, 2024
33f06a8
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
892e365
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
7858443
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
569cc1c
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 5, 2024
96a5566
Merge branch 'master' into master
Tuhinm2002 Oct 6, 2024
3b8d047
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 7, 2024
735077d
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 7, 2024
8819fdb
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
51db0c6
Merge branch 'master' into master
Tuhinm2002 Oct 7, 2024
6533338
Merge branch 'master' into master
Tuhinm2002 Oct 7, 2024
ed36d8a
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 7, 2024
13a3e7e
Merge branch 'master' into master
Tuhinm2002 Oct 7, 2024
697c108
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
002d55f
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 7, 2024
1536eeb
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
5288a5a
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
827b4d2
Update UniqueSubsequencesCountTest.java
Tuhinm2002 Oct 7, 2024
c7bd78a
Merge branch 'master' into master
Tuhinm2002 Oct 8, 2024
574fcf4
Update UniqueSubsequencesCount.java
Tuhinm2002 Oct 8, 2024
a505357
Merge branch 'master' into master
alxkm Oct 8, 2024
963201d
Merge branch 'TheAlgorithms:master' into master
Tuhinm2002 Oct 9, 2024
17f852d
Merge branch 'TheAlgorithms:master' into master
Tuhinm2002 Oct 9, 2024
2553434
feat : new bitmanipulation algo
Tuhinm2002 Oct 9, 2024
f3501d8
Merge branch 'TheAlgorithms:master' into master
Tuhinm2002 Oct 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}