Java icon indicating copy to clipboard operation
Java copied to clipboard

[FEATURE REQUEST] Find Single element in an array using Bit Manipulation

Open Tuhinm2002 opened this issue 1 year ago • 1 comments

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

Tuhinm2002 avatar Oct 09 '24 20:10 Tuhinm2002

@siriak hey check this interesting algo of bit manipulation. I created a PR at https://github.com/TheAlgorithms/Java/pull/5689

Tuhinm2002 avatar Oct 09 '24 20:10 Tuhinm2002