Skip to content

Instantly share code, notes, and snippets.

🎯
Focusing

Arivazhagan Ambigapathi arivazhagan

🎯
Focusing
Block or report user

Report or block arivazhagan

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@arivazhagan
arivazhagan / toSum.js
Created May 3, 2019
Find possible sum pairs in array
View toSum.js
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]);
}
View uniqueString.js
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')
View search.js
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) {
View countUniqueValues.js
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;
View maxArraySum.js
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];
View Multiple_pointer.js
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]];
View Same.js
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;
@arivazhagan
arivazhagan / Anagram.js
Last active Mar 27, 2019
Check given word is Anagram or not
View Anagram.js
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;
View BSTree.js
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);
View Tree.js
function Node(data) {
this.data = data;
this.parent = null;
this.children = [];
}
function Tree(data) {
var node = new Node(data);
this._root = node;
}
You can’t perform that action at this time.