Skip to content

Commit 2ac29c8

Browse files
committed
First Duplicate
1 parent c86d1ed commit 2ac29c8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

14_First-Duplicate/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// First Duplicate
2+
// Write a function that takes in an array of positive numbers and returns the first number that is duplicated. If no duplicates occur, return -1.
3+
4+
const firstDuplicate = numArr => {
5+
const containerObj = {}
6+
7+
for (const num of numArr) {
8+
if (containerObj[num]) {
9+
return num
10+
}
11+
containerObj[num] = 1
12+
}
13+
return -1
14+
}
15+
16+
console.log(firstDuplicate([2, 3, 3, 1, 5, 2]))
17+
console.log(firstDuplicate([2, 2]))
18+
console.log(firstDuplicate([2, 1, 3]))

0 commit comments

Comments
 (0)