Find the unique repeated element in the array

Source: Internet
Author: User

Find the unique repeated element in the array

[Problem]

1-1001 is placed in an array containing elements. Only one unique element value is repeated, and only one other element is displayed. Each array element can only be accessed once. design an algorithm to find it. Can you design an algorithm to implement it without supporting the storage space?

[Code]

# Include

 
  
# Include

  
   
# Include

   
    
/* According to the calculation method of the exclusive or method, after each two different numbers perform the exclusive or operation, the result is 1. After each two identical numbers are exclusive or, the result is 0, any number is different from zero or the result is still itself. Therefore, the number of N in array a [N] is different or the result is different from the result of 1 to N-1 or, and the obtained value is the expected value. */Int findRepeat1 (int * a, int len) {int I, res; res = a [0]; for (I = 1; I <len; I ++) {res ^ = I; res ^ = a [I];} return res;}/* uses the special relationship between the subscript and the content stored in the unit to traverse the access unit, once the accessed unit is assigned a tag, the tag is used as the key to discovering duplicate numbers. */Int findRepeat2 (int * a, int len) {int index; index = a [len-1]-1; while (1) {if (a [index] <0) break; a [index] * =-1; index = a [index] * (-1)-1 ;} return index + 1;} int main (void) {int a [] = {1, 2, 3, 4, 5, 6, 4}; int res; res = findRepeat2 (a, 7); printf ("% d \ n", res); return 0 ;}

   

  

 


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.