头图

Repeated numbers in the array

Problem: All numbers in an array nums of length n are in the range of 0~n-1. Some numbers in the array are repeated, but I don't know how many numbers are repeated, nor do I know how many times each number is repeated. Please find any duplicate number in the array.

Examples are as follows:

//输入:
[2, 3, 1, 0, 2, 5, 3]
//输出:2 或 3 
Limit: 2 <= n <= 100000

Thinking analysis

  • Method one: ordinary solution

We can create a new array, and then determine whether the new array contains nums , if not, add it to the new array, if there is, it means that the array item is duplicated, and then define another variable to receive the duplicate item. Then return. code show as below:

    var findRepeatNumber = function(nums) {
        let res = [],repeat = "",len = nums.length,i = 0;
        while(i < len){
            if(res.indexOf(nums[i]) === -1){
                res.push(nums[i]);
            }else{
                // 代表已经找到重复数字了,所以跳出循环
                repeat = nums[i];
                break;
            }
        }
        return repeat;
    }

The time complexity and space complexity of the above algorithm are analyzed as follows:

  • The time complexity is O(n).
  • The space complexity is O(n).

Method 2: In-situ replacement algorithm

We tried to think this way, because the title clearly stated that the range of the number in the array item is between 0~n-1 (note that this algorithm cannot be used without this condition, and this algorithm only uses time for space), also That is to say, for example, if the length of the array is 2, then the number of all array items in the array can only be 0 or 1. Therefore, we can guess that when the array subscript is equal to the number of the array item, it will not be repeated, if not If it is equal, then we make an exchange position between the number of the array item and the number equal to the subscript of the array. In the process of position exchange, when the two are equal, this indicates a duplication. For example, [1,1], the exchange position of the two will always be equal to the subscript of the array where 1 is located, and the duplicate number will be found.

    var findRepeatNumber = function(nums) {
        for(let i = 0,len = nums.length;i < len;i++){
            //定义一个中间变量用于交换
            let temp;
            while(nums[i] !== i){
                if(nums[i] === nums[nums[i]]){
                    // 判断数组项对应的数是否和数组数做下标对应的数一样,如果一样则重复
                    return nums[i];
                }
                // 开始做交换
                temp = nums[i];
                nums[i] = nums[temp];
                nums[temp] = temp;
            }
        }
        return -1;
    }

The time complexity and space complexity of the above algorithm are analyzed as follows:

  • Time complexity O(n): O(n) is used to traverse the array, and O(1) is used for judgment and exchange operations in each round of traversal.
  • Space complexity O(1): Use extra space with constant complexity.

More questions and solutions can be found in here .


夕水
5.2k 声望5.7k 粉丝

问之以是非而观其志,穷之以辞辩而观其变,资之以计谋而观其识,告知以祸难而观其勇,醉之以酒而观其性,临之以利而观其廉,期之以事而观其信。