Skip to content

Instantly share code, notes, and snippets.

View hk-skit's full-sized avatar
👨‍💻
On deep learning mode.

Hitesh Kumar hk-skit

👨‍💻
On deep learning mode.
View GitHub Profile
@hk-skit
hk-skit / js-oneliner.js
Last active January 9, 2024 23:46
Useful Array One-liners.
// Remove Duplicates from an array
const removeDuplicates =
arr => arr.filter((item, index) => index === arr.indexOf(item));
const removeDuplicates1 = array => [...new Set(array)];
const removeDuplicates2 = array => Array.from(new Set(array));
// Flattens an array(doesn't flatten deeply).
{
"React Function Component Typescript": {
"prefix": "rfct",
"body": [
"import type { FC } from \"react\"",
"import type { ${TM_FILENAME_BASE}Props } from \"./${TM_FILENAME_BASE}.types\"",
"",
"const ${TM_FILENAME_BASE}: FC<${TM_FILENAME_BASE}Props> = () => <div />;",
"",
"export default $TM_FILENAME_BASE;",
// Given a string and a set of delimiters, reverse the words in the string while maintaining
// the relative order of the delimiters. For example, given "hello/world:here", return "here/world:hello".
// Follow-up: Does your solution work for the following cases: "hello/world:here/", "hello//world:here"
const reverseWords = (string, delimiters) => {
const aux = [];
let word = '';
// O(n)
for (const str of string) {
if (!delimiters.has(str)) {
@hk-skit
hk-skit / spiral_matrix.js
Created May 24, 2021 11:10
Print matrix in spiral order
const Direction = {
L_R: 0,
T_B: 1,
R_L: 2,
B_T: 3,
};
const spiral = (matrix) => {
if (matrix.length === 0) {
return;
@hk-skit
hk-skit / Stack.ts
Last active December 7, 2020 21:23
Generic Stack in TypeScript
interface StackConstructor<T> {
new <T>(n: number): Stack<T>
}
export class Stack<T>{
private data: T[] = [];
static readonly OVERFLOW = 'OVERFLOW';
static readonly UNDERFLOW = 'UNDERFLOW';
constructor(private size: number) {
}
push(item: T): number | string {
export class BinaryTreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
/**
* Inserts node in level order.
*
import { BinaryTreeNode } from './BinaryTreeNode';
export class BinaryTree {
constructor() {
this.root = null;
}
get isEmpty() {
return this.root === null;
}
@hk-skit
hk-skit / Stack.js
Last active January 27, 2019 11:27
class Stack {
constructor() {
this.list = new LinkedList();
}
get isEmpty() {
return this.list.isEmpty;
}
/**
// Generator function.
*[Symbol.iterator]() {
let current = this.head;
while (current !== null) {
const { value } = current;
current = current.next;
yield value;
}
}
/**
* Iterator function.
*
* @returns
* @memberof LinkedList
*/
[Symbol.iterator]() {
let current = this.head;
const iterable = {
next: () => {