Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)_
- [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)_
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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<number>(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;
};
Loading