Only one occurrence of the number

Source: Internet
Author: User

Given An array of numbers nums , in which exactly-elements appear only once and all the other elements appear exactly Twice. Find the elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5] , return[3, 5]

classSolution { Public: Vector<int> Singlenumber (vector<int>&nums) {    intX_xor_y =0;  for(intn:nums) {x_xor_y^=N; }    //find the last one 1//1 indicates that the value of this point two is different, one is 1 is 1, otherwise it cannot be different or appear 1    intMask = x_xor_y & ~ (X_xor_y-1); intx =0; inty =0; //X:xor of A,a,b,b,x//Y:xor of C,c,y     for(intn:nums) {        if(N &mask) {x^=N; } Else{y^=N; }    }    returnvector<int>{x, y};}};

Given an array of integers, every element appears twice except for one. Find the single one.

class Solution {public:    int singlenumber (intint  N) {         int0;      for 0; I < n; + +i)        {^= a[i]    ;    } return num;    }};

Given an array of integers, every element appears three times except for one. Find the single one.

int singlenumber (vector<int> AA) {    int00;     // int size = sizeof (AA)/sizeof (int);     for (int0; i < aa.size (); i++) {        = (ones ^ aa[i]) & ~twos;         = (twos ^ aa[i]) & ~ones;    }     return ones;}

Only one occurrence of the number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.