Skip to content

Instantly share code, notes, and snippets.

@Kielx
Last active June 6, 2020 18:16
Show Gist options
  • Save Kielx/0ac7c59dd01349701e3fdb7aa9212381 to your computer and use it in GitHub Desktop.
Save Kielx/0ac7c59dd01349701e3fdb7aa9212381 to your computer and use it in GitHub Desktop.
Scrimba coding challanges
//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