Simple bit manipulation solution


  • 0
    X
    class Solution {
    public:
        int majorityElement(vector<int>& nums) {
            int res = INT_MAX;
            for(int n=0; n <= 31; n++) {
                int numOf0Bits=0;
                int numOf1Bits=1;
                for(auto num : nums) {
                    int bit=( num & (1 << n) ) >> n;
                    if(bit)
                        numOf1Bits++;
                    else
                        numOf0Bits++;
                }
                if(numOf1Bits > numOf0Bits) {
                    res = res | (1 << n);
                } else {
                    res = res & ( ~ ( 1 << n) );
                }
            }
            return res;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.