├── issubsequence.js ├── maxsubarraysum.js ├── arethereduplicates.js ├── minsubarraylen.js ├── samefrequency.js ├── averagepair.js ├── stack.js ├── findlongestsubstring.js ├── queue.js ├── stack1.js ├── binarysearchtree.js └── DoublyLinkedList.js /issubsequence.js: -------------------------------------------------------------------------------- 1 | function isSubsequence(str1, str2) { 2 | let i = 0; 3 | let j = 0; 4 | if (!str1) return true; 5 | 6 | while (j < str2.length) { 7 | if (str2[j] === str1[i]) i++; 8 | if (i === str1.length) return true; 9 | j++; 10 | } 11 | 12 | return false; 13 | } -------------------------------------------------------------------------------- /maxsubarraysum.js: -------------------------------------------------------------------------------- 1 | function maxSubarraySum(arr, num) { 2 | if (arr.length < num) return null; 3 | let maxSum = 0; 4 | let tempSum = 0; 5 | for (let i = 0; i < num; i++) { 6 | maxSum += arr[i]; 7 | } 8 | tempSum = maxSum; 9 | for (let i = num; i < arr.length; i++) { 10 | tempSum = tempSum - arr[i - num] + arr[i]; 11 | maxSum = Math.max(maxSum, tempSum); 12 | } 13 | return maxSum; 14 | } -------------------------------------------------------------------------------- /arethereduplicates.js: -------------------------------------------------------------------------------- 1 | function areThereDuplicates(...args) { 2 | args.sort(); 3 | let start = 0; 4 | let next = 1; 5 | while (next < args.length) { 6 | if (args[start] === args[next]) { 7 | return true; 8 | } 9 | start++; 10 | next++; 11 | } 12 | return false; 13 | } 14 | // Test cases 15 | console.log(areThereDuplicates(1, 2, 3)); // false 16 | console.log(areThereDuplicates(1, 2, 2)); // true 17 | console.log(areThereDuplicates('a', 'b', 'c', 'a')); // true -------------------------------------------------------------------------------- /minsubarraylen.js: -------------------------------------------------------------------------------- 1 | function minSubArrayLen(arr, num) { 2 | let minLength = Infinity; 3 | let start = 0; 4 | let end = 0; 5 | let sum = 0; 6 | 7 | while (end < arr.length) { 8 | sum += arr[end]; 9 | 10 | while (sum >= num) { 11 | minLength = Math.min(minLength, end - start + 1); 12 | sum -= arr[start]; 13 | start++; 14 | } 15 | end++; 16 | } 17 | 18 | return minLength === Infinity ? 0 : minLength; 19 | } 20 | -------------------------------------------------------------------------------- /samefrequency.js: -------------------------------------------------------------------------------- 1 | function sameFrequency(num1, num2) { 2 | let str1 = num1.toString(); 3 | let str2 = num2.toString(); 4 | if (str1.length !== str2.length) return false; 5 | let frequencyCounter = {}; 6 | 7 | for (let char of str1) { 8 | frequencyCounter[char] = (frequencyCounter[char] || 0) + 1; 9 | } 10 | for (let char of str2) { 11 | if (!frequencyCounter[char]) { 12 | return false; 13 | } else { 14 | frequencyCounter[char]--; 15 | } 16 | } 17 | return true; 18 | } -------------------------------------------------------------------------------- /averagepair.js: -------------------------------------------------------------------------------- 1 | function averagePair(arr, num) { 2 | if (arr.length === 0) return false; 3 | let left = 0; 4 | let right = arr.length - 1; 5 | while (left < right) { 6 | let avg = (arr[left] + arr[right]) / 2; 7 | if (avg === num) return true; 8 | else if (avg < num) left++; 9 | else right--; 10 | } 11 | return false; 12 | } 13 | // Test cases 14 | console.log(averagePair([1, 2, 3], 2.5)); // true 15 | console.log(averagePair([1, 3, 3, 5, 6, 7, 10, 12, 19], 8)); // true 16 | console.log(averagePair([-1, 0, 3, 4, 5, 6], 4.1)); // false 17 | console.log(averagePair([1, 4], 3)); // false -------------------------------------------------------------------------------- /stack.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(value){ 3 | this.value = value; 4 | this.next = null; 5 | } 6 | } 7 | 8 | class Stack { 9 | constructor(){ 10 | this.first = null; 11 | this.last = null; 12 | this.size = 0; 13 | } 14 | 15 | // add 16 | push(val){ 17 | var newNode = new Node(val); 18 | if(!this.first){ 19 | this.first = newNode; 20 | this.last = newNode; 21 | } else { 22 | var temp = this.first; 23 | this.first = newNode; 24 | this.first.next = temp; 25 | } 26 | return ++this.size; 27 | } 28 | } -------------------------------------------------------------------------------- /findlongestsubstring.js: -------------------------------------------------------------------------------- 1 | function findLongestSubstring(str) { 2 | let longest = 0; 3 | let seen = {}; 4 | let start = 0; 5 | 6 | for (let end = 0; end < str.length; end++) { 7 | let char = str[end]; 8 | if (seen[char] !== undefined) { 9 | start = Math.max(start, seen[char] + 1); 10 | } 11 | seen[char] = end; 12 | longest = Math.max(longest, end - start + 1); 13 | } 14 | 15 | return longest; 16 | } 17 | 18 | // Test cases 19 | console.log(findLongestSubstring('')) // 0 20 | console.log(findLongestSubstring('rithmschool')) // 7 21 | console.log(findLongestSubstring('thisisawesome')) // 6 22 | console.log(findLongestSubstring('thecatinthehat')) // 7 23 | console.log(findLongestSubstring('bbbbbb')) // 1 24 | console.log(findLongestSubstring('longestsubstring')) // 8 25 | console.log(findLongestSubstring('thisishowwedoit')) // 6 -------------------------------------------------------------------------------- /queue.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(value) { 3 | this.value = value; 4 | this.next = null; 5 | } 6 | } 7 | 8 | class Queue { 9 | constructor() { 10 | this.first = null; 11 | this.last = null; 12 | this.size = 0; 13 | } 14 | 15 | // add in end queue 16 | enqueue(val) { 17 | var newNode = new Node(val); 18 | if (!this.first) { 19 | this.first = newNode; 20 | this.last = newNode; 21 | } else { 22 | this.last.next = newNode; 23 | this.last = newNode; 24 | } 25 | return ++this.size; 26 | } 27 | 28 | // del in first queue 29 | dequeue() { 30 | if (!this.first) return null; 31 | 32 | var temp = this.first; 33 | if (this.first === this.last) { 34 | this.last = null; 35 | } 36 | this.first = this.first.next; 37 | this.size--; 38 | return temp.value; 39 | } 40 | } -------------------------------------------------------------------------------- /stack1.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(value){ 3 | this.value = value; 4 | this.next = null; 5 | } 6 | } 7 | 8 | class Stack { 9 | constructor(){ 10 | this.first = null; 11 | this.last = null; 12 | this.size = 0; 13 | } 14 | 15 | // add 16 | push(val){ 17 | var newNode = new Node(val); 18 | if(!this.first){ 19 | this.first = newNode; 20 | this.last = newNode; 21 | } else { 22 | var temp = this.first; 23 | this.first = newNode; 24 | this.first.next = temp; 25 | } 26 | return ++this.size; 27 | } 28 | 29 | // del 30 | pop(){ 31 | if(!this.first) return null; 32 | 33 | var temp = this.first; 34 | if(this.first === this.last) { 35 | this.last = null; 36 | } 37 | this.first = this.first.next; 38 | this.size--; 39 | return temp.value; 40 | } 41 | } -------------------------------------------------------------------------------- /binarysearchtree.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(value){ 3 | this.value = value; 4 | this.left = null; 5 | this.right = null; 6 | } 7 | } 8 | class BinarySearchTree { 9 | constructor(){ 10 | this.root = null; 11 | } 12 | insert(value){ 13 | var newNode = new Node(value); 14 | if(this.root === null){ 15 | this.root = newNode; 16 | return this; 17 | } 18 | var current = this.root; 19 | while(true){ 20 | if(value === current.value) return undefined; 21 | if(value < current.value){ 22 | if(current.left === null){ 23 | current.left = newNode; 24 | return this; 25 | } 26 | current = current.left; 27 | } else { 28 | if(current.right === null){ 29 | current.right = newNode; 30 | return this; 31 | } 32 | current = current.right; 33 | } 34 | } 35 | } 36 | find(value){ 37 | if(this.root === null) return undefined; 38 | var current = this.root; 39 | while(current){ 40 | if(value === current.value) return current; 41 | if(value < current.value){ 42 | current = current.left; 43 | } else { 44 | current = current.right; 45 | } 46 | } 47 | return undefined; 48 | } 49 | contains(value){ 50 | if(this.root === null) return false; 51 | var current = this.root; 52 | while(current){ 53 | if(value === current.value) return true; 54 | if(value < current.value){ 55 | current = current.left; 56 | } else { 57 | current = current.right; 58 | } 59 | } 60 | return false; 61 | } 62 | } -------------------------------------------------------------------------------- /DoublyLinkedList.js: -------------------------------------------------------------------------------- 1 | class Node { 2 | constructor(val) { 3 | this.val = val; 4 | this.next = null; 5 | this.prev = null; 6 | } 7 | } 8 | 9 | class DoublyLinkedList { 10 | constructor() { 11 | this.head = null; 12 | this.tail = null; 13 | this.length = 0; 14 | } 15 | 16 | // add in end list 17 | push(val) { 18 | const newNode = new Node(val); 19 | if (this.length === 0) { 20 | this.head = newNode; 21 | this.tail = newNode; 22 | } else { 23 | this.tail.next = newNode; 24 | newNode.prev = this.tail; 25 | this.tail = newNode; 26 | } 27 | this.length++; 28 | return this; 29 | } 30 | 31 | // del in end list 32 | pop() { 33 | if (this.length === 0) return undefined; 34 | const removedNode = this.tail; 35 | if (this.length === 1) { 36 | this.head = null; 37 | this.tail = null; 38 | } else { 39 | this.tail = removedNode.prev; 40 | this.tail.next = null; 41 | removedNode.prev = null; 42 | } 43 | this.length--; 44 | return removedNode; 45 | } 46 | 47 | // del in first list 48 | shift() { 49 | if (this.length === 0) return undefined; 50 | const removedNode = this.head; 51 | if (this.length === 1) { 52 | this.head = null; 53 | this.tail = null; 54 | } else { 55 | this.head = removedNode.next; 56 | this.head.prev = null; 57 | removedNode.next = null; 58 | } 59 | this.length--; 60 | return removedNode; 61 | } 62 | 63 | // add in first list 64 | unshift(val) { 65 | const newNode = new Node(val); 66 | if (this.length === 0) { 67 | this.head = newNode; 68 | this.tail = newNode; 69 | } else { 70 | this.head.prev = newNode; 71 | newNode.next = this.head; 72 | this.head = newNode; 73 | } 74 | this.length++; 75 | return this; 76 | } 77 | 78 | // get 79 | get(index) { 80 | if (index < 0 || index >= this.length) return null; 81 | let current, count; 82 | if (index <= this.length / 2) { 83 | current = this.head; 84 | count = 0; 85 | while (count !== index) { 86 | current = current.next; 87 | count++; 88 | } 89 | } else { 90 | current = this.tail; 91 | count = this.length - 1; 92 | while (count !== index) { 93 | current = current.prev; 94 | count--; 95 | } 96 | } 97 | return current; 98 | } 99 | 100 | // change 101 | set(index, val) { 102 | const foundNode = this.get(index); 103 | if (foundNode != null) { 104 | foundNode.val = val; 105 | return true; 106 | } 107 | return false; 108 | } 109 | 110 | // add 111 | insert(index, val) { 112 | if (index < 0 || index > this.length) return false; 113 | if (index === 0) return !!this.unshift(val); 114 | if (index === this.length) return !!this.push(val); 115 | 116 | const newNode = new Node(val); 117 | const beforeNode = this.get(index - 1); 118 | const afterNode = beforeNode.next; 119 | 120 | beforeNode.next = newNode; 121 | newNode.prev = beforeNode; 122 | newNode.next = afterNode; 123 | afterNode.prev = newNode; 124 | 125 | this.length++; 126 | return true; 127 | } 128 | } --------------------------------------------------------------------------------