In C language programming, only two numbers appear in one array, and all other numbers appear twice.

Source: Internet
Author: User

In C language programming, only two numbers appear in one array, and all other numbers appear twice.

In C language programming, only two numbers appear in one array, and all other numbers appear twice.

Code Implementation (Environment: Visual Studio 2017)

// Only two numbers appear in an array once, and all other numbers appear twice. // Find the two numbers and program them. # Include

 
  
# Include

  
   
Void find (int a [], int sz) {int I = 0; int num1 = 0; int num2 = 0; int num = 0; int flag = 0; for (I = 0; I <sz; I ++) {num = num ^ a [I] ;}for (I = 0; I <32; I ++) {if (num> I) & 1 )! = 1) // returns the first position of 1 in the binary number of this digit after the occurrence of an exception or {flag ++;} else break;} for (I = 0; I <sz; I ++) {if (a [I]> flag) & 1) = 1) // divide it into two groups num1 ^ = a [I]; else num2 ^ = a [I];} printf ("% d \ n", num1, num2);} int main () {int a [] = {1, 2, 2, 6, 6, 4, 4, 5, 8, 5}; int sz = sizeof (a)/sizeof (a [0]); find (, sz); system ("pause"); return 0 ;}

  

 

Related Article

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.