// Initializeconstarr=[];constarr=newArray(size).fill(0);// [0,0,0,0,0]constarr=Array.from({length:n},(_,i)=>i);// [0,1,2,...,n-1]// Basic Operationsarr.push(element);// Add to endarr.pop();// Remove from endarr.unshift(element);// Add to startarr.shift();// Remove from start// Slicing and Splicingarr.slice(startIdx,endIdx);// Returns new array, endIdx not includedarr.splice(startIdx,deleteCount,...itemsToAdd);// Common Methodsarr.map(x=>x*2);// Returns new arrayarr.filter(x=>x>0);// Returns new arrayarr.reduce((acc,curr)=>acc+curr,initialValue);arr.sort((a,b)=>a-b);// Ascendingarr.reverse();arr.join('');// Convert to stringarr.includes(element);// Check existencearr.indexOf(element);// First occurrencearr.lastIndexOf(element);// Last occurrence
String Operations
// Creation and Accessconststr="hello";str.length;str[0]orstr.charAt(0);// Common Methodsstr.substring(startIdx,endIdx);// endIdx not includedstr.substr(startIdx,length);// Deprecated but good to knowstr.slice(startIdx,endIdx);// Can use negative indicesstr.split('');// Convert to arraystr.toLowerCase();str.toUpperCase();str.trim();// Remove whitespacestr.replace(old,new);str.replaceAll(old,new);str.startsWith(prefix);str.endsWith(suffix);str.includes(substr);str.repeat(count);
Map and Set
// Mapconstmap=newMap();map.set(key,value);map.get(key);map.has(key);map.delete(key);map.clear();map.size;// Setconstset=newSet();set.add(value);set.has(value);set.delete(value);set.clear();set.size;// Object as HashMapconstobj={};obj[key]=value;keyinobj;// Check existencedeleteobj[key];Object.keys(obj);Object.values(obj);Object.entries(obj);
// Queue using Arrayconstqueue=[];queue.push(element);// enqueuequeue.shift();// dequeue// Stack using Arrayconststack=[];stack.push(element);stack.pop();// LinkedList NodeclassListNode{constructor(val=0,next=null){this.val=val;this.next=next;}}// Binary Tree NodeclassTreeNode{constructor(val=0,left=null,right=null){this.val=val;this.left=left;this.right=right;}}// Trie NodeclassTrieNode{constructor(){this.children=newMap();this.isEndOfWord=false;}}
Bit Manipulation
// Common Operationsn<<1;// Multiply by 2n>>1;// Divide by 2n&1;// Check if oddn&(n-1);// Remove last set bitn&-n;// Get last set bitn|(1<<pos);// Set bit at positionn&~(1<<pos);// Clear bit at positionn^(1<<pos);// Toggle bit at position
Common Patterns and Utilities
// Number OperationsMath.max(...arr);Math.min(...arr);Math.floor(n);Math.ceil(n);Math.abs(n);Number.MAX_SAFE_INTEGER;Number.MIN_SAFE_INTEGER;Infinity;-Infinity;// Random NumberMath.random();// [0, 1)Math.floor(Math.random()*n);// [0, n-1]// Character Code'a'.charCodeAt(0);// 97String.fromCharCode(97);// 'a'// Check TypeNumber.isInteger(n);Array.isArray(arr);typeofvariable;// ParsingparseInt(str);parseFloat(str);
Common Interview Patterns
// Two Pointersletleft=0,right=arr.length-1;while (left<right){// processleft++;right--;}// Sliding Windowletleft=0;for (letright=0;right<arr.length;right++){// add arr[right] to windowwhile (/* window condition */){// remove arr[left] from windowleft++;}}// Binary Searchletleft=0,right=arr.length-1;while (left<=right){constmid=Math.floor((left+right)/2);if (arr[mid]===target)returnmid;if (arr[mid]<target)left=mid+1;elseright=mid-1;}
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)