BackmediumHashingAccenture

Longest Consecutive Sequence Solution

Problem Statement

Given an unsorted array of integers, find the length of the longest consecutive sequence.

Example 1
Input
[0, 3, 7, 2, 5, 8, 4, 6, 0, 1]
Output
9

Explanation: Step-by-step: with input [0, 3, 7, 2, 5, 8, 4, 6, 0, 1], we first remove duplicates by converting the array to a set. Then we find the minimum number in the set. We iterate over the set starting from the minimum number and check if the current number plus one is in the set. If it is, we increment the sequence length. We keep doing this until we find a number that is not in the set or we have checked all numbers in the set. The length of the longest sequence is the maximum sequence length found.

Example 2
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output
10

Explanation: Step-by-step: with input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], we first remove duplicates by converting the array to a set. Then we find the minimum number in the set. We iterate over the set starting from the minimum number and check if the current number plus one is in the set. If it is, we increment the sequence length. We keep doing this until we find a number that is not in the set or we have checked all numbers in the set. The length of the longest sequence is the maximum sequence length found.

Constraints

  • 0 <= n <= 10^5
  • -10^9 <= arr[i] <= 10^9
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Longest Consecutive Sequence — Problem Statement & Solution Guide

HashingMediumHash Set
TimeO(n)
|
SpaceO(n)

Problem Description

Given an unsorted array of integers, find the length of the longest consecutive sequence.

Examples

Example 1

Input

[0, 3, 7, 2, 5, 8, 4, 6, 0, 1]

Output

9

Explanation: Step-by-step: with input [0, 3, 7, 2, 5, 8, 4, 6, 0, 1], we first remove duplicates by converting the array to a set. Then we find the minimum number in the set. We iterate over the set starting from the minimum number and check if the current number plus one is in the set. If it is, we increment the sequence length. We keep doing this until we find a number that is not in the set or we have checked all numbers in the set. The length of the longest sequence is the maximum sequence length found.

Example 2

Input

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Output

10

Explanation: Step-by-step: with input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], we first remove duplicates by converting the array to a set. Then we find the minimum number in the set. We iterate over the set starting from the minimum number and check if the current number plus one is in the set. If it is, we increment the sequence length. We keep doing this until we find a number that is not in the set or we have checked all numbers in the set. The length of the longest sequence is the maximum sequence length found.

Constraints

  • 0 <= n <= 10^5
  • -10^9 <= arr[i] <= 10^9

Optimal Approach & Strategy

Use HashSet. For each element, check if it's the start of a sequence (if num-1 is not in set). If it is, count upwards in the set. Time O(N), Space O(N).

Brute Force Approach

Sort the array and count consecutive elements. Time O(N log N).

Verified Code Solutions

JavaScript Solution
Time: O(n)
function longestConsecutive(nums) { let numSet = new Set(nums); let longestStreak = 0; for (let num of numSet) { if (!numSet.has(num - 1)) { let currentNum = num; let currentStreak = 1; while (numSet.has(currentNum + 1)) { currentNum += 1; currentStreak += 1; } longestStreak = Math.max(longestStreak, currentStreak); } } return longestStreak; }

Asked in Top Tech Interviews

Accenture

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.