Except two numbers in an integer array, the other numbers appear twice. Please write a program to find the numbers that only appear once.

Source: Internet
Author: User

First, let's start with a simple question: In an array, all the numbers except one appear twice. How can we find the different number,

Simply put, all numbers are different or the final result is the number to be searched, because the same number is different or the result is 0.

Now let's think about this question:

The book "The beauty of programming" provides a way to convert all numbers into an exclusive or, get a number, and then use a non-zero value of the number as a filter bit, divide the array into two parts. The number displayed at this time is divided into different parts. Now the problem has been converted to one:

AlgorithmAs follows:

Public int [] findtwodiff (INT [] A) {int [] result = new int [2]; int temp = 0; int flag = 1; for (int t:) {temp ^ = T;} while (temp & flag) = 0) {flag <= 1 ;}for (int t:) {If (T & flag) = 1) {result [0] ^ = T;} else {result [1] ^ = T ;}} return result ;}

 

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.