Solution with BFS and explained (javascript) (99.68%)
Hi,\n\nI found this solution using BFS to determine the Longest Consecutive Sequence. In this case, it is possible to describe the elements in nums as nodes, an
Hi,\n\nI found this solution using BFS to determine the Longest Consecutive Sequence. In this case, it is possible to describe the elements in `nums` as nodes, and the solution is to find the largest connected graph.\n\nIn the `First Loop`, I created a hashmap that will describe every node that I had in `nums` as `NOT_VISITED`.\n\nIn the `Second Loop`, we iterate again in the `nums` and add the first `NOT_VISITED` node to the queue. This loop makes sure that we visit every node in `nums`.\n\nIn `While statement` it is defined the `currentNode` as `VISITED`, and also add +1 to the connected graph counter. After that, it tries to add to the `queue` the previous number and the next one as well (if the node exists and it is not visited yet).\n\nFinishing the `While statement`, we compare the old results to the new one selecting the largest from these values, and return it!\n\nThe complexity stills O(2n) => O(n) because the algorithmn is accessing at least two times the elements in `nums`.\n\nThat\'s it, if you find difficulty, please let me know, I\'ll try my best to explain to you. =)\n\n```javascript\nlet NOT_VISITED = "NOT_VISITED";\nlet VISITED = "VISITED";\n\nvar longestConsecutive = function(nums) {\n\t// First loop\n    let nodes = {};\n    for(let i = 0; i < nums.length; i++) {\n        nodes[nums[i]] = NOT_VISITED;\n    }\n    \n    let max = 0;\n   \n\t// Second loop\n    for (let i = 0; i < nums.length; i++) {\n        if (nodes[nums[i]] === VISITED) {\n            continue;\n        }\n        \n        let queue = [nums[i]];\n        let newMax = 0;\n        \n\t\t// Applying BFS\n        while(queue.length > 0) {\n            let currentNode = queue.shift();\n\n            nodes[currentNode] = VISITED;\n            newMax++;\n\n            if (nodes[currentNode + 1] === NOT_VISITED) {\n                queue.push(currentNode + 1);\n            }\n            if (nodes[currentNode - 1] === NOT_VISITED){\n                queue.push(currentNode - 1);\n            }\n        }\n        max = Math.max(max, newMax);\n    }\n    \n    return max;\n};\n```
['Breadth-First Search', 'JavaScript']