To find duplicate values in an array

Source: Internet
Author: User

Title Description:

Suppose you use an array of 1001 integers that are arbitrarily arranged, but you know that all integers are between 1 and 1000 (including 1000), and that all other numbers appear only once, except that one number appears two times. Suppose you can only handle this integer once, and use an algorithm to find the duplicate number. (no secondary storage mode)


1.sum the number,then subtract the sum from 1001*1002/2 (i.e. 1+2+3+..........+1001);

2. Using a XOR a XOR b=b; (A xor a =0)

The code is as follows:

int findx (int a[])
{
int k = a[0];
for (int i = 1; i <=; i++)
K~= A[i]~i;
return k;
}

To find duplicate values in an 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.