Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View mohitsharma93's full-sized avatar

Mohit Sharma mohitsharma93

  • walkover.in
  • indore
View GitHub Profile
@mohitsharma93
mohitsharma93 / singleLinkedList.js
Created May 8, 2021 12:24
single linked list in JavaScript
/**
* linked list is a linear data structure similar to an array. However, unlike arrays,
* elements are not stored in a particular memory location or index. Rather each element is a separate
* object that contains a pointer or a link to the next object in that list.
* for ex: head--> [D, reference to next] [D, reference to next] [D, null] --> tail
* D belongs to data.
* The head is a reference to the first node in the linked list.
* The last node on the list points to null. If a list is empty, the head is a null reference
*/
@mohitsharma93
mohitsharma93 / Stack.js
Last active May 8, 2021 13:55
JavaScript presentation of stack data structure
/**
* A stack is useful when we want to add data in sequential order and remove data. Based on its definition,
* a stack can remove only the most recently added data.
*
* stack is work on LAST IN FIRST OUT principle.
* stack is just like array with only capabilities of push and pop (method).
* [6] ----> which is last element of stack
* [5]
* [4]
* [3]
@mohitsharma93
mohitsharma93 / Queue.js
Last active May 8, 2021 14:55
JavaScript presentation of queue data structure
/**
* A queue can remove only the most recently added value, what if we need to remove old value. here comes another
* data structure QUEUE.
* queue is work on FIRST IN FIRST OUT
*
* queue is work on LAST IN FIRST OUT principle.
* queue is just like array with only capabilities of push and pop (method).
* [6]
* [5]
* [4]
@mohitsharma93
mohitsharma93 / debounce.js
Last active September 26, 2022 16:06
debounce is decorator wrapper that suspends calls to f until there’s ms milliseconds of inactivity
/**
* use this function if you want to call other function after some
* time(provided by you in ms parameter) with latest arguments.
* Basically it delay our functino call by given MS(milliseconds).
*/
function debounce(fun,ms) {
let time; // storing timer returned by setTimeout api.
return function() {
clearTimeout(time); // clear timer on each time of function call.
time = setTimeout(() => fun(...arguments), ms)
@mohitsharma93
mohitsharma93 / bubbleSort.js
Created October 28, 2022 13:06
Bubble sort (JavaScript)sort array to Asc/Desc order
const arr = [-2, 45, 0, 11, -9];
/*
* Bubble sort is a sorting algorithm that compares two adjacent elements and swaps them until they are in the intended order.
*/
function bubbleSort(arr) {
// loop to access each array element
for (let i = 0; i < arr.length; i++) {
// keep track of swapping
@mohitsharma93
mohitsharma93 / selectionSort.js
Created October 28, 2022 14:04
Selection Sort (JavaScript) sort array by Asc/Desc
const arr = [2, 12, -100, -15, -2];
/**
* Selection sort is a sorting algorithm that selects the smallest element from an unsorted list in each iteration
* and places that element at the beginning of the unsorted list.
* @param {*} arr
* @returns Array sorted.
* Uses:
* 1. checking of all the elements is compulsory.
* 2. cost of swapping does not matter.
@mohitsharma93
mohitsharma93 / parentheses.js
Created September 23, 2023 08:26
question : Given a string s containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’ determine if the input string is valid.
/**
* question : Given a string s containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’,
* determine if the input string is valid.
* An input string is valid if: Open brackets must be
* closed by the same type of brackets. Open brackets must be closed in the correct order.
*/
let parenthese = "()[]{}"
function checkByReplace(s) {
while(true) {
let len = s.length;