Find two occurrences of two times in an array _ find two occurrences of two repetitions in an array

Source: Internet
Author: User
Tags array length
The title is as follows: The existing array length is n+1, which contains 1 to n-2, the order is uncertain, of which two numbers appeared two times, now to find out the two digits.

Example A={2, 3, 1, 4, 5, 2, 4}, this array length is 7, storing 1 to 5, but 2 and 4 appear two times, program output 2 and 4.

Method 1 Brute Force search

Main idea: For the number of I in the array, look for all the integers i+1 to the end, a number if it occurs two times, you can find the second occurrence of the number after the first time.
Time complexity O (n^2)

Space complexity O (1)

Method 2: Exclusive OR (XOR)

Main idea: Because the number between 1 and N is limited, and each number appears at least once, you can first of all the integers in the array, and then the result and 1, 2, 3 、、、 n different or again, so that the two repeated occurrences of the integer of the different or result x. The main thing to do is to differentiate between the two, and for the difference or result x, its binary representation has 0 and 1 components, by the nature of the XOR, it is known that the digits in the binary represented by 0 are the result of two duplicates corresponding to 1 or 0, while the digits of 1 have only one possibility: two numbers correspond to position one is 0 , one is 1. With this feature, we can select a specific position (where x is 1) to divide the original array into two parts, part I corresponds to the number of a particular position of 1, and Part II corresponds to the number of that particular position of 0, which translates the problem into: finding a recurring number in each section.

Time complexity O (n)
Space complexity O (1)

#include <stdio.h>//method 1//void find_duplicates (int *num, int start, int end)//{//int size = End-start +
1;
int i = 0;
int j = 0; for (i = 0; i < size; i++)//{/for (j = i + 1; j < size; J +)//{//if (num[i] = = Num[j])//Print
F ("%d\n", Num[i]);
	}///////}//method 2 void find_duplicates (int *num, int start, int end) {int size = End-start + 1;
	int bit_flag = 0;
	int i = 0;
	for (i = 0; i < size; i++) {Bit_flag ^= num[i];
	for (i = 1; i < size-1 i++) {Bit_flag ^= i;

	///According to the rightmost 1 in the Bit_flag binary, divide the integers in the array into two parts int division_bit = Bit_flag & ~ (bit_flag-1); int a = 0;//partial i xor result int b = 0;//Partial II's XOR result for (i = 0; i < size; i++) {if (num[i) & Division_bit) a ^= nu
		M[i];
	else b ^= Num[i];
		for (i = 1; i < size-1 i++) {if (I & division_bit) a ^= i;
	else b ^= i;
printf ("Duplicate numbers a=%d \ t b=%d\n", A, b); } void Main () {int a[] = {2, 3, 1, 4, 5, 2, 4};
	Find_duplicates (A, 0, 6);


 }


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.