Skip to content

Instantly share code, notes, and snippets.

@ovarunendra
Created August 5, 2018 12:10
Show Gist options
  • Save ovarunendra/d48d1fa7b6983a36ebaebe52ada22ad8 to your computer and use it in GitHub Desktop.
Save ovarunendra/d48d1fa7b6983a36ebaebe52ada22ad8 to your computer and use it in GitHub Desktop.
JS Bin findLongestConseqSubseq // source https://jsbin.com/facezoq
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="findLongestConseqSubseq">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
const findLongestConseqSubseq = (arr) => {
let s = new Set();
let ans = 0;
for (let elm of arr) {
s.add(elm);
}
for (let elm of arr) {
if(!s.has(elm-1)) {
let j = elm;
while(s.has(j)) {
j += 1;
}
ans = Math.max(ans, j-elm);
}
}
return ans;
}
const arr = [36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42];
console.log(findLongestConseqSubseq(arr))
</script>
<script id="jsbin-source-javascript" type="text/javascript">const findLongestConseqSubseq = (arr) => {
let s = new Set();
let ans = 0;
for (let elm of arr) {
s.add(elm);
}
for (let elm of arr) {
if(!s.has(elm-1)) {
let j = elm;
while(s.has(j)) {
j += 1;
}
ans = Math.max(ans, j-elm);
}
}
return ans;
}
const arr = [36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42];
console.log(findLongestConseqSubseq(arr))</script></body>
</html>
const findLongestConseqSubseq = (arr) => {
let s = new Set();
let ans = 0;
for (let elm of arr) {
s.add(elm);
}
for (let elm of arr) {
if(!s.has(elm-1)) {
let j = elm;
while(s.has(j)) {
j += 1;
}
ans = Math.max(ans, j-elm);
}
}
return ans;
}
const arr = [36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42];
console.log(findLongestConseqSubseq(arr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment