-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Closed
Labels
Description
What would you like to Propose?
An optimized way of finding single element in the array of duplicate elements (appearing twice).
- Time complexity : O(n)
- Space complexity : O(1)
*Test case
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public final class SingleElementTest {
@Test
void testSingleElementOne(){
int[] arr = {1,1,2,2,4,4,3};
assertEquals(3,SingleElement.findSingleElement(arr));
}
@Test
void testSingleElementTwo(){
int[] arr = {1,2,2,3,3};
assertEquals(1,SingleElement.findSingleElement(arr));
}
@Test
void testSingleElementThree(){
int[] arr = {10};
assertEquals(10,SingleElement.findSingleElement(arr));
}
}
Issue details
The existing code base is missing this important yet easy and fun way of finding single element from an array of duplicate element using XOR
operation. It takes O(n) time complexity and O(1) space complexity.
Additional Information
No response