Skip to content

Instantly share code, notes, and snippets.

View IAMIronmanSam's full-sized avatar
🎯
Focusing

Tony Stark IAMIronmanSam

🎯
Focusing
View GitHub Profile
@IAMIronmanSam
IAMIronmanSam / toSum.js
Created May 3, 2019 04:19
Find possible sum pairs in array
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]);
}
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')
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) {
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;
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];
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]];
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;
@IAMIronmanSam
IAMIronmanSam / Anagram.js
Last active March 27, 2019 06:38
Check given word is Anagram or not
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;
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);
function Node(data) {
this.data = data;
this.parent = null;
this.children = [];
}
function Tree(data) {
var node = new Node(data);
this._root = node;
}