Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b57fdfe

Browse files
find-the-duplicate-number
1 parent a48d04c commit b57fdfe

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var findDuplicate = function(nums) {
6+
for(let i=0;i<nums.length;i++){
7+
if(nums.filter(num => num === nums[i]).length > 1) return nums[i];
8+
}
9+
};
10+
11+
var findDuplicate1 = function(nums) {
12+
let mySet = new Set();
13+
for(let i=0;i<nums.length;i++){
14+
if(mySet.has(nums[i]))
15+
return nums[i]
16+
mySet.add(nums[i])
17+
18+
}
19+
};
20+
21+
var findDuplicate3 = function(nums) {
22+
let myMap = {};
23+
for(let i=0;i<nums.length;i++){
24+
if(myMap[nums[i]] != undefined)
25+
return nums[i]
26+
myMap[nums[i]] = i;
27+
}
28+
};
29+
30+
console.log(findDuplicate3([1,3,4,2,2]))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Find the Duplicate Number
2+
3+
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
4+
5+
## Example 1
6+
7+
Input: [1,3,4,2,2]
8+
9+
Output: 2
10+
11+
## Example 2
12+
13+
Input: [3,1,3,4,2]
14+
15+
Output: 3
16+
17+
## Note
18+
19+
You must not modify the array (assume the array is read only).
20+
21+
You must use only constant, O(1) extra space.
22+
23+
Your runtime complexity should be less than O(n^2).
24+
25+
There is only one duplicate number in the array, but it could be repeated more than once.
26+
27+
## More Info
28+
29+
<https://leetcode.com/problems/find-the-duplicate-number/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /