The sword refers to the offer surface question (Java edition): Duplicate numbers in an array

Source: Internet
Author: User

Title: All numbers in an array of length n are within the range of 0 to n-1. Some of the numbers in the array are duplicates, but it is not known that several numbers have been duplicated, and the number of repetitions of each number is unknown. 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.

An easy way to solve this problem is to sort the input arrays first. It is easy to find duplicate numbers from a sorted array, just scan the sorted array from beginning to end. Sorting an array of length n takes time O (Nlogn) time.

You can also use a hash table to solve this problem, sequentially scan each number in the array, each scan to a number, you can use O (1) time to determine whether the hash table contains the number. If the hash table does not have this number, add it to the hash table. If the number already exists in the hash table, then a duplicate number is found. The time complexity of this algorithm is O (n), but it increases the time complexity at the cost of a hash table of size O (n). Let's see if there is an algorithm with a time complexity of O (1).

We notice that the numbers in the array are within the range of 0 to n-1. If there are no duplicate numbers in this array, then the number I will appear in the position labeled I when the array is sorted. Because there are duplicate numbers in the array, some locations may have numbers, while some locations may not have numbers.

Now let's rearrange this array. Rice seedlings every number in this array from beginning to end. When scanning to a number labeled I, first compare this number (denoted by m) is not I. If it is, then scan the next number. If not, compare it with the number of M numbers. If it is equal to the number m, a duplicate number is found (the number appears in the position of the subscript I and M). If it and the number m do not want to wait, the first and second numbers are exchanged, and the M is placed in its place. Next, repeat the comparison, the exchange process, until a duplicate number is found.

Use the array {2,3,1,0,2,5,3} as an example to analyze the steps to find duplicate numbers. The No. 0 number of the array (counting from 0, and the subscript of the arrays) is 2, and its subscript does not want to wait, so it and the subscript 2 of the number 1 exchange, the exchange of the array is {1,3,2,0,2,5,3}. At this point the No. 0 number is 1, still with its subscript do not want to wait, continue to put it and subscript 1 of the number 3 exchange, get the array {0,1,2,3,2,5,3}. At this point the No. 0 digit is 0, and then the next number is scanned, and in the next few numbers, the three digits of the next number are three-to-one, and their subscripts and values are equal, so no action is required. Next scan the number labeled 4 to 2. Since its value is not labeled with its subscript, compare it with the number labeled 2. Notice that the number labeled 2 in the array is also 2, which is the number 2 and the 4 positions of the subscript 2 and subscript two, so a duplicate number is found.

The Java code is implemented as follows:

/** * Duplicate digits in array */package swordforoffer;/** * @author Jinshuangqi * * August 12, 2015 */public class E51duplicationinarray {Publ IC Boolean duplicate (int[] arr) {if (arr== null | | arr.length <= 0) {return false;} for (int i = 0;i<arr.length;i++) {if (Arr[i] < 0 | | arr[i] > ARR.LENGTH-1) return false;} for (int i = 0;i<arr.length;i++) {while (arr[i]! = i) {if (arr[i] = = Arr[arr[i]) {System.out.println (arr[i]); return true ;} Else{int temp = arr[i];arr[i] = arr[temp];arr[temp] = temp;}}} return false;} public static void Main (string[] args) {int[] arr = {2,3,1,0,2,5,3}; E51duplicationinarray test = new E51duplicationinarray (); System.out.println (Test.duplicate (arr));}}

. In the code, although there are one or two repetitions, but each number can be exchanged up to two times to find their own location, so the total time complexity is O (n), in addition, all the operation steps are on the input array, do not need to allocate additional memory, so the space complexity is O (1).


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The sword refers to the offer surface question (Java edition): Duplicate numbers in an array

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.