Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / remove-element.js
Created January 20, 2017 02:33
Remove element: Given an array and a value, remove all instances of that value in place and return the new length.
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function(nums, val) {
for (var i=nums.length - 1; i >= 0; i--) {
if (nums[i] === val) {
nums.splice(i, 1);
}
@primaryobjects
primaryobjects / palindrome-product.js
Created January 20, 2017 02:30
Largest palindrome made from the product of two n-digit numbers.
var isPalindrome = function(x) {
return x.toString() == x.toString().split("").reverse().join("");
};
/**
* @param {number} n
* @return {number}
*/
var largestPalindrome = function(n) {
var value = Math.pow(10, n) - 1;
@primaryobjects
primaryobjects / substring.js
Last active March 8, 2023 23:39
Longest Substring Without Repeating Characters in javascript.
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
var max = 0;
var str = '';
var i = 0;
var cache = [];
@primaryobjects
primaryobjects / rotate-NxN.js
Last active September 25, 2018 06:42
Rotate an NxN matrix by 90 degrees.
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var rotate = function(matrix) {
// reverse the rows
matrix = matrix.reverse();
// swap the symmetric elements
for (var i = 0; i < matrix.length; i++) {
@primaryobjects
primaryobjects / ksmallest.js
Last active January 19, 2017 03:38
Kth Smallest Element in a BST, using Inorder Traversal.
// See also http://www.geeksforgeeks.org/find-k-th-smallest-element-in-bst-order-statistics-in-bst/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
@primaryobjects
primaryobjects / palindrome.js
Last active January 18, 2017 21:29
Checking if a number is a palindrome, without converting to a string. Demo at http://jsbin.com/panukukeda/edit?js,console
function getNthDigit(val, n)
{
var modVal = val % Math.pow(10,n);//Remove all digits larger than nth
return Math.floor(modVal / Math.pow(10,n-1));//Remove all digits less than nth
}
/**
* @param {number} x
* @return {boolean}
*/
@primaryobjects
primaryobjects / combinatorics.js
Last active February 12, 2017 14:49
Combinations and permutations in Javascript. Demo at http://jsbin.com/xufomalori/edit?js,console
// Combinations and permutations.
// Counts can be verified at http://www.calculatorsoup.com/calculators/discretemathematics/permutations.php
// noprotect
var combinations = function(arr) {
var result = [];
for (var i=0; i<arr.length; i++) {
var item1 = arr[i];
@primaryobjects
primaryobjects / 1-closure.js
Last active February 28, 2017 19:46
Example of a Javascript class and closure. Demo https://jsbin.com/pahaxazopo/edit?js,console
/* jshint esnext: true */
var Person = class {
constructor(first, last) {
this.first = first;
this.last = last;
}
toString() {
return this.first + ' ' + this.last;
@primaryobjects
primaryobjects / countAndSay.js
Last active January 19, 2017 13:51
Count and say sequence generator: 1, 11, 21, 1211, 111221.
var generate = function(n) {
var result = '';
n += '';
var digit = n[0];
var count = 1;
for (var i=1; i<n.length; i++) {
var s = n[i];
@primaryobjects
primaryobjects / quickSort.js
Last active January 20, 2017 02:36
JavaScript QuickSort, Quick sort. Demo at http://jsbin.com/kejicuhaku/edit?js,console
function quickSort(arr) {
var sorted = [];
var left = [];
var right = [];
if (arr && arr.length > 1) {
// 1. Select a random number.
var mid = arr[0];
// 2. Place all numbers less in the left array, greater in the right array.