Created
August 22, 2021 14:15
-
-
Save pavanesh2021/cfd97c48cb5711bbfc2b0e71af1769a8 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/rapolon
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var arr = [7,7,6,2,3,3,4]; // >> print first duplicate i.e 1 | |
// find first duplicate value | |
function findFirstDuplicate(a){ | |
let mySet = new Set(); | |
for(let i=0; i <a.length; i++ ){ | |
if(mySet.has(a[i])){ | |
return a[i]; | |
}else{ | |
mySet.add(a[i]); | |
} | |
} | |
return -1; | |
} | |
console.log(findFirstDuplicate(arr)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var arr = [7,7,6,2,3,3,4]; // >> print first duplicate i.e 1 | |
// find first duplicate value | |
function findFirstDuplicate(a){ | |
let mySet = new Set(); | |
for(let i=0; i <a.length; i++ ){ | |
if(mySet.has(a[i])){ | |
return a[i]; | |
}else{ | |
mySet.add(a[i]); | |
} | |
} | |
return -1; | |
} | |
console.log(findFirstDuplicate(arr)); | |
</script></body> | |
</html> |
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
var arr = [7,7,6,2,3,3,4]; // >> print first duplicate i.e 1 | |
// find first duplicate value | |
function findFirstDuplicate(a){ | |
let mySet = new Set(); | |
for(let i=0; i <a.length; i++ ){ | |
if(mySet.has(a[i])){ | |
return a[i]; | |
}else{ | |
mySet.add(a[i]); | |
} | |
} | |
return -1; | |
} | |
console.log(findFirstDuplicate(arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment