Hi there, my name is Alex, I’ve been using Glide for a few years now! I have always tried to refrain from posting questions because I don’t want to waste anyone’s time. But alas, I’m not a “real programmer” and I’ve hit a WALL! I’ve put maybe 8 hours into it already and I know its probably an easy fix but I just can’t figure it out, so I’m finally asking for help!
Here’s what I’m trying to accomplish: I have an array of numbers and I want to find the maximum consecutive numbers in the array. For example, an array of [2,3,4,6,8,10] would yield a result of 3, because 2,3,4, are consecutive numbers. I found a javaScript function online that claims to solve this but I cannot get it working. Again, I am NOT a programmer so I really don’t understand what it is saying or where potential problems might be.
I have attached pictures as well as the code I am currently using in the JavaScript column. I would GREATLY appreciate any input or advice anyone is willing to give. Thank you so much!
var arr = p1 ;
var n = arr.length;
function findLongestConseqSubseq(arr, n) {
let S = new Set();
for (let i = 0; i < n; i++)
S.add(arr[i]);
let ans = 0;
for (let i = 0; i < n; i++) {
if (!S.has(arr[i] - 1)) {
let j = arr[i];
while (S.has(j))
j++;
ans = Math.max(ans, j - arr[i]);
}
}
return ans;
}
return findLongestConseqSubseq(arr, n);