From e338daabda21c06d090f0ff967e5c8b1b9f8bbad Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Sat, 25 Jul 2026 20:05:16 +0300 Subject: [PATCH 1/3] test(neetcode|roadmap|longest-consecutive-sequence): testcases --- .../solution.test.ts | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.test.ts diff --git a/src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.test.ts b/src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.test.ts new file mode 100644 index 0000000..a5bae89 --- /dev/null +++ b/src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.test.ts @@ -0,0 +1,38 @@ +import { longestConsecutive } from './solution'; + +describe('Longest Consecutive Sequence | NeetCode RoadMap | Testcases', () => { + test('#1 Subsequence from start to end', () => { + const nums = [2, 20, 4, 10, 3, 4, 5]; + const expected = 4; + const output = longestConsecutive(nums); + expect(output).toBe(expected); + }); + + test('#2 Subsequence unsorted', () => { + const nums = [0, 3, 2, 5, 4, 6, 1, 1]; + const expected = 7; + const output = longestConsecutive(nums); + expect(output).toBe(expected); + }); + + test('#3 One element', () => { + const nums = [2]; + const expected = 1; + const output = longestConsecutive(nums); + expect(output).toBe(expected); + }); + + test('#4 Empty array', () => { + const nums: number[] = []; + const expected = 0; + const output = longestConsecutive(nums); + expect(output).toBe(expected); + }); + + test('#5 Negative nums', () => { + const nums = [9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6]; + const expected = 7; + const output = longestConsecutive(nums); + expect(output).toBe(expected); + }); +}); From 62d4e5f01c625a9943d88e9f335a7f8dd6d5ef01 Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Sat, 25 Jul 2026 20:05:35 +0300 Subject: [PATCH 2/3] feat(neetcode|roadmap|longest-consecutive-sequence): hash set solution --- .../longest-consecutive-sequence/solution.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.ts diff --git a/src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.ts b/src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.ts new file mode 100644 index 0000000..33e4398 --- /dev/null +++ b/src/neetcode/arrays-&-hashing/longest-consecutive-sequence/solution.ts @@ -0,0 +1,21 @@ +/** + * @param {number[]} nums - an array of integers + * @return {number} - the length of the longest consecutive sequence of elements + * (sequence of elements in which each element is exactly 1 greater than the previous element) that can be formed. + */ +export const longestConsecutive = (nums: number[]): number => { + const unique = new Set(nums); + let mx = 0; + + for (const num of unique) { + if (!unique.has(num - 1)) { + let current = 1; + while (unique.has(num + current)) { + current++; + } + mx = Math.max(mx, current); + } + } + + return mx; +}; From d240e47fc0ec0c18e3fb2c9f21076e7fc11ba5d1 Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Sat, 25 Jul 2026 20:05:46 +0300 Subject: [PATCH 3/3] feat(neetcode|roadmap|docs): update table of contents --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3db290f..cd3e466 100644 --- a/README.md +++ b/README.md @@ -77,4 +77,5 @@ - [Task #5](src/neetcode/arrays-&-hashing/top-k-frequent-elements) - _Top K Frequent Elements_ - _[check the task](https://neetcode.io/problems/top-k-elements-in-list)_ - [Task #6](src/neetcode/arrays-&-hashing/encode-and-decode-strings) - _Encode And Decode Strings_ - _[check the task](https://neetcode.io/problems/string-encode-and-decode)_ - [Task #7](src/neetcode/arrays-&-hashing/product-of-array-except-self) - _Products of Array Except Self_ - _[check the task](https://neetcode.io/problems/products-of-array-discluding-self)_ - - [Task #8](src/neetcode/arrays-&-hashing/valid-sudoku) - _Valid Sudoku_ - _[check the task](https://neetcode.io/problems/valid-sudoku)_ \ No newline at end of file + - [Task #8](src/neetcode/arrays-&-hashing/valid-sudoku) - _Valid Sudoku_ - _[check the task](https://neetcode.io/problems/valid-sudoku)_ + - [Task #9](src/neetcode/arrays-&-hashing/longest-consecutive-sequence) - _Longest Consecutive Sequence_ - _[check the task](https://neetcode.io/problems/longest-consecutive-sequence)_ \ No newline at end of file