[Sword refers to Offer learning] [interview question 51: Repeated numbers in the array], sword refers to offer

Source: Internet
Author: User

[Sword refers to Offer learning] [interview question 51: Repeated numbers in the array], sword refers to offer
Question: All numbers in an array with a length of n are in the range of 0 to n-1. Some numbers in the array are repeated, but I do not know how many numbers are repeated or how many times each number is repeated. Find any repeated number in the array.Example

For example, if the input length is 7 in an array {2, 3, 3}, the corresponding output is a repeated Number 2 or 3.

Question Analysis

An easy way to solve this problem is to sort the input array first. It is easy to find the repeated numeric time from the sorted array. You only need to scan the sorted array from start to end. It takes O (nlogn) Time to sort an array with a length of n.
Hash Tables can also be used to solve this problem. Each number of arrays is scanned sequentially from start to end. Each time a number is scanned, the O (1) time can be used to determine whether the number is included in the hash table. If the number does not exist in the hash table, add it to the hash table. If the number already exists in the hash table, a duplicate number is found. The time complexity of this algorithm is O (n), but it increases the time efficiency at the cost of an O (n) hash table. Let's see if there are any algorithms with the space complexity of O (1.
We noticed that all numbers in the array are in the range 0 to n-1. If there are no repeated numbers in this array, after the array is sorted, the number I will appear at the position marked as I. Because there are repeated numbers in the array, some locations may have multiple numbers, and some locations may have no numbers.
Now let's re-arrange the array and scan every number in the array from start to end. When a number with a subscript of I is scanned, first compare whether the number (expressed in m) is equal to I. If yes, scan the next number. If not, compare it with the m number.
If it is equal to the number m, a duplicate number is found (the number appears at the position marked as I and m ). If it is not the same as the m number, it exchanges the I number with the m number and places m in its position. Next, repeat the comparison and exchange process until we find a duplicate number.
Take the Array {2, 3, 1, 0, 2, 5, 3} as an example to analyze the steps for finding repeated numbers. The first number of the array (counting from 0, consistent with the subscript of the array) is 2, which is not equal to its subscript, so it is exchanged with the number 1 with the subscript of 2. The exchanged array is {1.3.2.0.2.5.3 }. At this time, the first number is 1 and it is still not equal to its subscript. Continue to exchange it with the number 3 with the subscript of 1 and get the Array {0th, 3 }. next, exchange 0th numbers 3 and 3rd numbers 0 to obtain the Array {0, 1, 2, 2, 5, 3 }. At this time, the number of the 0th digits is 0, and then the next digit is scanned. In the following numbers, the three numbers whose subscript is 1, 2, and 3 are respectively 1, 2, and 3. Their subscript and value are equal, so no operation is required. Next, we will scan the number with the subscript 4. Because its value is not equal to its subscript, We will compare it with the number with the subscript 2. Note that the number marked as 2 in the array is also 2, that is, the number appears at the two positions marked as 2 and subscript 4, so we can find a repeated number.

Code Implementation
Public class Test51 {/*** title: All numbers in an array with a length of n are in the range of 0 to n-1. Some numbers in the array are repeated. * I do not know how many numbers are repeated or how many times each number is repeated. Find any repeated number in the array. * For example, if the input length is 7 in an array {2, 3, 3}, the corresponding output is a duplicate number 2 or. ** @ Param number * @ return */public static int duplicate (int [] number) {if (number = null | number. length <1) {return-1;} // determines whether the input is in [0, number. for (int I: number) {if (I <0 | I> = number. length) {return-1 ;}}for (int I = 0; I <number. length; I ++) {// when number [I] is different from I, the while (number [I]! = I) {// if the I position is the same as the number at the number [I] position, there are repeated numbers if (number [I] = number [number [I]) {return number [I] ;}// if different, else {swap (number, I, number [I]) ;}} return-1 ;} private static void swap (int [] data, int x, int y) {int tmp = data [x]; data [x] = data [y]; data [y] = tmp;} public static void main (String [] args) {int [] numbers1 = {2, 1, 3, 1, 4}; System. out. println (duplicate (numbers1); int [] numbers2 = {2, 4, 3, 1, 4}; System. out. println (duplicate (numbers2); int [] numbers3 = {2, 4, 2, 1, 4}; System. out. println (duplicate (numbers3); int [] numbers4 = {2, 1, 3, 0, 4}; System. out. println (duplicate (numbers4); int [] numbers5 = {2, 1, 3, 5, 4}; System. out. println (duplicate (numbers5 ));}}
Running result

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.