Skip to content

Instantly share code, notes, and snippets.

View daartv's full-sized avatar

Dario Arteaga daartv

  • Vio.com (Formerly FindHotel)
  • Amsterdam, The Netherlands
View GitHub Profile
@daartv
daartv / FrequencySort.js
Last active April 22, 2017 17:03
Frequency Sort and Is Subset Of
var frequencySort = function(s) {
var arr = s.split('');
var obj = {};
var results = [];
for (var i = 0; i < arr.length; i++) {
obj[arr[i]] = 0
}
for (var j = 0; j < arr.length; j++) {
if (obj[arr[j]] !== undefined) {
obj[arr[j]]++;
@daartv
daartv / messageBus.js
Created April 13, 2017 16:51
Implement a Message Bus
let messageBus = {
topics: {},
subscribe: (topic, listener) => {
if (!this.topics[topic]) {
this.topics[topic] = []
}
this.topics[topic].push(listener)
},
@daartv
daartv / vowelDoubler.js
Created April 10, 2017 16:23
Double all the vocals on an array of string characters
var vowelDoubler = (array) => {
for (var i = 0; i < array.length; i++) {
if (array[i] === 'a' || array[i] === 'e' || array[i] === 'i' || array[i] === 'o' || array[i] === 'u') {
array.splice(i, 0, array[i])
i++
}
}
return array;
}
@daartv
daartv / ProductExceptSelf.js
Created April 6, 2017 16:49
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]
var productExceptSelf = function(nums) {
//Declare a results array
var results = [];
//iterate through the input array
for (var i = 0; i < nums.length; i++) {
//Save the value in the current index in a temporary variable
var current = nums[i];
//get that number out of the array
nums.splice(i,1)
//reduce the remaining numbers and push it into results
@daartv
daartv / kthSmallestElementBST
Created April 3, 2017 16:55
Find the Kth smallest element on a Binary Search Tree
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} k
@daartv
daartv / russianDollEnvelope.js
Created March 16, 2017 16:59
Russian Doll Envelope
var maxEnvelopes = function(envelopes) {
for (var i = 0; i < envelopes.length; i++) {
for (var j = 0; j < envelopes.length - 1; j++) {
if (envelopes[j][0] > envelopes[j+1][0] && envelopes[j][1] > envelopes[j+1][1]) {
var temp = envelopes[j+1];
envelopes[j+1] = envelopes[j];
envelopes[j] = temp;
}
}
}