Last active
June 6, 2020 18:16
-
-
Save Kielx/0ac7c59dd01349701e3fdb7aa9212381 to your computer and use it in GitHub Desktop.
Scrimba coding challanges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//function to find first duplicate in array | |
function firstDuplicate(nums) { | |
let MyObj = {} | |
let myArr = [] | |
for (i=0; i< nums.length ; i++){ | |
for (j=i+1; j< nums.length; j++){ | |
if(nums[i] === nums[j]){ | |
MyObj[nums[i]] = j; | |
} | |
} | |
} | |
if (Object.keys(MyObj).length){ | |
myArr = Object.values(MyObj) | |
const min = Math.min(...myArr) | |
return(nums[min]) | |
} | |
else{ | |
return -1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment