Use a hash table to reduce the complexity of time

Source: Internet
Author: User
Tags array length hash
The title describes all the numbers in an array of length n in the range of 0 to n-1. Some of the numbers in the array are duplicates, but it is not known that several numbers are duplicates. I don't know how many times each number repeats. Please find any duplicate numbers in the array. For example, if you enter an array of length 7 {2,3,1,0,2,5,3}, then the corresponding output is a repeating number of 2 or 3.  
 1/* 2 * Ideas: 3 * Find duplicate numbers in an array: 4 * This problem has a well-defined array of numbers that are not larger than the length of the array and are greater than 0, so you can do so with a hash table, so that the memory will not be out of bounds 5 * 6 * bitmap
 : 7 * A bitmap array with an array length is applied and initialized to 0, just as the index of each number can be indexed as an array, and then the value of the bitmap array appears as the number of times the index position numbers appear.
 8 * Finally sweep through the bitmap array, find the first value greater than 1.
9 * 10 * can be used in the Java data structure HashMap do, but there is no meaning, no use of the characteristics of the topic.
11 * Programming is a special case to consider a particular data structure.  * * * Public boolean duplicate (int numbers[],int length,int [] duplication)-{if (length <= 1) (+ +) (false;) + int bitmapnumber[] = new Int[lengt
h];          
(int i = 0; i < length; i++) (bitmapnumber[i] = 0; 26} 27         (int i = 0; i < length; i++) {bitmapnumber[numbers[i]]++; 31 } for (int i = 0; i < length; i++) (Bitmapnumber[i ] > 1) [duplication[0] = I
Max. return true;
A. " 46}

 

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.