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
9 changes: 9 additions & 0 deletions contains-duplicate/nrudev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function containsDuplicate(nums: number[]): boolean {
const map = new Map<number, number>();

for (let i = 0; i < nums.length; i++) {
if (map.has(nums[i])) return true;
map.set(nums[i], 1);
}
return false;
}
13 changes: 13 additions & 0 deletions longest-consecutive-sequence/nrudev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function longestConsecutive(nums: number[]): number {
const set = new Set<number>(nums);
let result = 0;

for (const num of set) {
if (set.has(num - 1)) continue;
let length = 1;
while (set.has(num + length)) length++;
result = Math.max(length, result);
}

return result;
}
13 changes: 13 additions & 0 deletions top-k-frequent-elements/nrudev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function topKFrequent(nums: number[], k: number): number[] {
const map: Map<number, number> = new Map();

nums.forEach((val) => {
if (map.has(val)) map.set(val, map.get(val)!! + 1);
else map.set(val, 1);
});

return Array.from(map)
.sort((a, b) => b[1] - a[1])
.slice(0, k)
.map((item) => item[0]);
}
13 changes: 13 additions & 0 deletions two-sum/nrudev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function twoSum(nums: number[], target: number): number[] {
const result: number[] = [];

for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
result.push(i, j);
}
}
}

return result;
}