Skip to content

Commit

Permalink
return first dupe number
Browse files Browse the repository at this point in the history
  • Loading branch information
spences10 committed Dec 11, 2017
1 parent cd7fc24 commit 0b64184
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,25 @@ function addEven() {

addEven(arr)
```

### Return the first duplicate number

```js
function firstDuplicate(arr) {
// empty array to use to check incoming array against
let checkArray = {}

// loop it
for (let i = 0; i < arr.length; i++) {
// check that array element against
// checkArray
if (checkArray[arr[i]] !== undefined)
// if there's no matching item then
return arr[i]
else
// append to the checkArray
checkArray[arr[i]] = i
}
return -1
}
```

0 comments on commit 0b64184

Please sign in to comment.