Longest Consecutive Sequence

Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
let set = new Set()    for(let num of nums) {
set.add(num)
}
longestConsecutive = (nums) => {
let set = new Set()
for(let num of nums) {
set.add(num)
}

let longestSeq = 0
for(let num of set) {
if (!set.has(num - 1)){
let currentNum = num
let currentSeq = 1

while (set.has(currentNum + 1)) {
currentSeq += 1
currentNum += 1
}
longestSeq = Math.max(longestSeq, currentSeq)
}
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Public cloud performance measurement report

Automation in a corporate environment using Microsoft technologies, Part 2

Uber Interview Question — LeetCode 1334

Using TUN devices to create a simple VPN like Tunnel

Which one is best to use — Realm or Room?

CS373 Blog 4/5/20

Here is my referral link for BEE Network.

✨Metachess IDO Whitelist🔥

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nerly Ton

Nerly Ton

More from Medium

Machine Learning Based Thread Pool Tuning via Program Analysis

Choosing data storage devices: USB, HDD/SSD, NAS

Creating a Game Application Using HMS

Cultivate. Optimize. Develop. Enhance.