Find out only 1 occurrences of the number in the array

Source: Internet
Author: User
Tags bitwise
Any number XOR itself equals 0, two repetitions of the number XOR, and then only 1 digits left.
Knowledge Point: Any number XOR 0 equals itself
Bitwise AND A&B only the two sides are 1 for 1
Bitwise OR | One of the 1 is 1.
Bitwise XOR ^ The same for both sides is 0 different for 1
Note: This method is only suitable for other numbers to appear even if the number of times

public class Test_plus {    public static int findNotDouble(int[] a) {        int n=a.length;        int result=a[0];        int i;        for(i=1;i<n;++i)            result ^= a[i];        return result;    }    public static void main(String[] args) {        int array[]= {1,2,3,2,4,3,5,4,1};        int num = findNotDouble(array);        System.out.println(num);    }}

Find out only 1 occurrences of the number in the array

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.