Find the element that appears once

Given an array where every element occurs three times, except one element which occurs only once. Find the element that occurs once.

The expected time complexity is O(n) and O(1) extra space.

 Example:

Input: arr[]={2,3,3,4,4,3,4}
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int findNumberAppearsOnce(vector<int&nums)
{
    int first = 0, second = 0, third;
    int n = nums.size();

    //iterate till the end of array
    for (int i = 0; i < n; i++)
    {
        //update second
        second = second | (first & nums[i]);

        //update first
        first = first ^ nums[i];

        //update third
        third = ~(first & second);

        //update first
        first = first & third;
        second = second & third;
    }
    return first;
}

int main()
{
    vector<int> nums = {2334434};
    cout << findNumberAppearsOnce(nums);
    return 0;
}


No comments:

Post a Comment