This file contains 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
function toSum(numArray,sum){ | |
let pairs = []; | |
let hash = []; | |
for(let i = 0;i < numArray.length; i++){ | |
let counterPart = sum - numArray[i]; | |
if(hash.indexOf(counterPart) != -1){ | |
pairs.push([numArray[i],counterPart]); | |
} | |
hash.push(numArray[i]); | |
} |
This file contains 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
function countUniqueString(arr){ | |
const lookup = {}; | |
for (let letter of arr) { | |
// if letter exists, increment, otherwise set to 1 | |
lookup[letter] ? lookup[letter] += 1 : lookup[letter] = 1; | |
} | |
return Object.keys(lookup); | |
} | |
countUniqueString('aabbbbbcccdefffff') |
This file contains 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
function search(array, val) { | |
let min = 0; | |
let max = array.length - 1; | |
while (min <= max) { | |
let middle = Math.floor((min + max) / 2); | |
let currentElement = array[middle]; | |
if (array[middle] < val) { |
This file contains 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
function countUniqueValues(arr){ | |
if(arr.length === 0) return 0; | |
var i = 0; | |
for(var j = 1; j < arr.length; j++){ | |
if(arr[i] !== arr[j]){ | |
i++; | |
arr[i] = arr[j] | |
} | |
} | |
return i + 1; |
This file contains 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
function maxSubarraySum(arr, num){ | |
let maxSum = 0; | |
let tempSum = 0; | |
if (arr.length < num) return null; | |
for (let i = 0; i < num; i++) { | |
maxSum += arr[i]; | |
} | |
tempSum = maxSum; | |
for (let i = num; i < arr.length; i++) { | |
tempSum = tempSum - arr[i - num] + arr[i]; |
This file contains 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
function sumZero(arr){ | |
let left = 0; | |
let right = arr.length - 1; | |
//Moving from left and right to middle | |
while(left < right){ | |
//First element + Last element | |
let sum = arr[left] + arr [right]; | |
if(sum === 0){ | |
//Found a first matching pair | |
return [arr[left], arr[right]]; |
This file contains 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
function same (string1,string2){ | |
let FC1 = {}; | |
let FC2 = {}; | |
//Short Circuit if length is differnt | |
if(string1.length !== string2.length){ | |
return false; | |
} | |
//Categorize the input data to compare | |
for(let val of string1){ | |
FC1[val] = (FC1[val]||0)+1; |
This file contains 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
function validAnagram(first, second) { | |
if (first.length !== second.length) { | |
return false; | |
} | |
const lookup = {}; | |
for (let letter of first) { | |
// if letter exists, increment, otherwise set to 1 | |
lookup[letter] ? lookup[letter] += 1 : lookup[letter] = 1; |
This file contains 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
var Tree = function(data) { | |
this.data = data; | |
this.left = null; | |
this.right = null; | |
} | |
Tree.prototype.insert = function(value){ | |
if(value <= this.data){ | |
if(this.left == null){ | |
this.left = new Tree(value); |
This file contains 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
function Node(data) { | |
this.data = data; | |
this.parent = null; | |
this.children = []; | |
} | |
function Tree(data) { | |
var node = new Node(data); | |
this._root = node; | |
} |
NewerOlder