Interview Questions: Find repeated numbers from the array of 1001 integer Elements

Source: Internet
Author: User

Original article, copyright belongs to Hu Tian Fa (hutianfa@163.com) All, reprint please indicate the source:
Http://blog.csdn.net/aidisheng/archive/2008/09/24/2972497.aspx

 

Question:

There is an array with a length of 1001 (Suppose it is A), which stores the one thousand integers from 1 to unordered, and an element is an integer from 1, use the best algorithm to find the repeated integer.

 

My answer (implemented in Ruby ):

  1. #
  2. # Program: Find repeated numbers from the array of 1001 integer Elements
  3. # Author: Hu Tian Fa (hutianfa@163.com)
  4. #
  5. Def find_duplicate Array
  6. H = {}
  7. Array. Each do | I |
  8. If H. Include? I. to_s.to_sym
  9. Puts "find I:" + I. to_s
  10. Return
  11. Else
  12. H. Merge! I. to_s.to_sym => I
  13. End
  14. End
  15. End

Test:

  1. A = [1, 2, 3, 4, 5, 4]
  2. Find_duplicate

Output result:

  1. Find I: 4

 

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.