Daily Microsoft face question--day 7 (find only two occurrences in the array)

Source: Internet
Author: User
Tags hash

< The following Microsoft face questions are all from the network >

< The following answers and analysis are purely personal points of view, shortcomings, but also hope to point out ^_^ >

< copyright, reprint do not forget to indicate the source:http://blog.csdn.net/zhanxinhang>

Q : Suppose you have an array of 1001 integers that are arbitrarily arranged, but you know that all integers are between 1 and 1000 (including 1000). In addition, all other numbers appear only once, except that one number appears two times. Suppose you can only handle this array once , and use an algorithm to find the duplicate number. If you use secondary storage in your calculations, can you find an algorithm that doesn't work this way?

Analysis :

Method One, if the use of secondary storage, which way to choose how to store it. Can use a hash of the storage, with 1 to 1000 as the index of the hash table, traverse the original array, statistics the number of occurrences of the number and stored in the number as the index value of the hash table, if a hash[x] value is 2 exit the loop, X is repeated two times the number. The time complexity is the worst of O (N). Advantages: High efficiency, disadvantage: the memory space consumed is too large. The code is as follows:

int fun1 (const int a[])
{
  int hash[1002]={0};
  int x=0;
  for (int i = 0; i<1001; i++)
    {
      if ((++hash[a[i]]) = = 2)
      	{
      	  x = a[i];
      	  break;
      	}
    }
  return x;
}

Method Two, if you do not use the auxiliary storage method. An array of 1001 integers is known to have only one number that appears two times, and the integers are between 1 and 1000, so you can push all the numbers within the group that contain 1 to 1000. 1000] and an X that appears two times is any number from 1 to 1000. This allows you to calculate the sum of all the numbers in the original array S1 and arithmetic progression [...] 1000] and S2, and then calculates the difference between S1 and S2, the difference is the number x that occurs two times in the original array. The time complexity is fixed o (n). Pros and Cons: memory space consumption is almost no, but efficiency is lost in the use of hash table storage. The code is as follows:

int fun2 (const int a[])
{
  int s1=0,s2;
  s2 = 1001*1000/2; 
  for (int i = 0; i<1001; i++)
    {
      s1+=a[i];
    }
  return s1-s2;
}


Prev: Daily Microsoft face question--day 6 (print all symmetric substrings)

======= Welcome to my homepage (Http://blog.csdn.net/zhanxinhang) to has a communication =======


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.