Posts Find the element that appears once in sorted array (geeksforgeeks - SDE Sheet)
Post
Cancel

Find the element that appears once in sorted array (geeksforgeeks - SDE Sheet)

PROBLEM DESCRIPTION

Given a sorted array arr[] of size N. Find the element that appears only once in the array. All other elements appear exactly twice.

geeksforgeeks

SOLUTION

1
2
3
4
5
6
7
8
9
10
class Solution
{
    int findOnce(int arr[], int n)
    {
        int x = arr[0];
        for(int i=1; i<n; i++)
            x = x ^ arr[i];
        return x;
    }
}
This post is licensed under CC BY 4.0 by the author.