Topic describes
Find duplicate numbers in the array.
All numbers in an array of length N, nums, are in the range 0 to n-1. Some numbers in the array are repeated, but we don’t know how many are repeated, or how many times each number is repeated. Please find any duplicate number in the array.
The subject sample
Input:2.3.1.0.2.5.3] output:2 或 3
Copy the code
Subject analysis
To find duplicate numbers in an array, use the Map method, using key-value pairs
Code implementation
/ * * *@param {number[]} nums
* @return {number}* /
var findRepeatNumber = function(nums) {
const map = new Map(a);for (let i = 0; i < nums.length; i++) {
if (map.has(nums[i])) return nums[i]
map.set(nums[i], 1)}return null;
};
Copy the code
Title source
LeetCode refers to the duplicate number in Offer 03