This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://practice.geeksforgeeks.org/problems/number-of-occurrence/0 | |
| // https://www.geeksforgeeks.org/count-number-of-occurrences-or-frequency-in-a-sorted-array/ | |
| function numberOfOccurrence(arr, targetNum) { | |
| return arr.filter((num) => num === targetNum).length || -1; | |
| } | |
| const nums1 = [1,1,2,2,2,2,3]; | |
| const result1 = numberOfOccurrence(nums1,2); | |
| console.log(result1 === 4); | |
| const nums2 = [1,1,2,2,2,2,3]; | |
| const result2 = numberOfOccurrence(nums2,4); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://practice.geeksforgeeks.org/problems/stickler-theif/0 | |
| function sticklerTheif(houses) { | |
| function getAmount(condition) { | |
| return (acc, house, idx) => { | |
| if (condition(idx)) { | |
| acc += house; | |
| } | |
| return acc; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/ | |
| // https://practice.geeksforgeeks.org/problems/k-largest-elements/0 | |
| function kLargest(arr, n, k) { | |
| return arr.sort((a, b) => b - a).splice(0, k); | |
| } | |
| const arrEx1 = [12,5,787,1,23]; | |
| const resEx1 = kLargest(arrEx1, arrEx1.length, 2); | |
| console.log(resEx1.join(',') === '787,23'); | |
| const arrEx2 = [1,23,12,9,30,2,50]; | |
| const resEx2 = kLargest(arrEx2, arrEx2.length, 3); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Program for array rotation | |
| // https://www.geeksforgeeks.org/array-rotation/ | |
| function rotate(arr, d, n) { | |
| // Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements. | |
| const stash = arr.splice(0, d); | |
| return arr.concat(stash); | |
| } | |
| const arr = [1,2,3,4,5,6,7]; | |
| const res = rotate(arr, 2, arr.length); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Given a sorted circularly linked list of Nodes that store integers and a new Node, Insert the new Node into the correct position. (Duplicates allowed) | |
| class Node { | |
| constructor(val, next) { | |
| this.val = val; | |
| this.next = next; | |
| } | |
| } | |
| function printList(head) { | |
| let tmp = head; | |
| let arr = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/ | |
| class Node { | |
| constructor(val, next) { | |
| this.val = val; | |
| this.next = next; | |
| } | |
| } | |
| function printList(head) { | |
| let tmp = head; | |
| let arr = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://www.geeksforgeeks.org/flattening-a-linked-list/ | |
| // https://www.youtube.com/watch?v=PSKZJDtitZw | |
| function printList(root) { | |
| let tmp = root; | |
| let arr = []; | |
| while (tmp) { | |
| arr.push(tmp.val); | |
| tmp = tmp.down; | |
| } | |
| return arr; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://www.geeksforgeeks.org/detect-and-remove-loop-in-a-linked-list/ | |
| function printList(root) { | |
| let tmp = root; | |
| let arr = []; | |
| while (tmp) { | |
| arr.push(tmp.val); | |
| tmp = tmp.next; | |
| } | |
| return arr; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://www.geeksforgeeks.org/add-two-numbers-represented-by-linked-lists-set-3/ | |
| // Same - https://www.geeksforgeeks.org/add-two-numbers-represented-by-linked-lists/ | |
| function printList(root) { | |
| let tmp = root; | |
| let arr = []; | |
| while (tmp) { | |
| arr.push(tmp.val); | |
| tmp = tmp.next; | |
| } | |
| return arr; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://www.geeksforgeeks.org/linked-list-set-1-introduction/ | |
| class Node { | |
| constructor(val, next) { | |
| this.val = val; | |
| this.next = next; | |
| } | |
| } | |
| function printList(list) { | |
| // O(N) |