Skip to content

Instantly share code, notes, and snippets.

View 4skinSkywalker's full-sized avatar
♥️
Stay strong guys!

Fred's GitHub 4skinSkywalker

♥️
Stay strong guys!
View GitHub Profile
function countdown(num) {
console.log(num)
// Base Case with return inside
if (num === 0)
return
// recursive call with a different input
countdown(num - 1)
function egg() {
chicken()
}
function chicken() {
egg()
}
chicken() // your PC explodes and a new universe is born
function maxKAdjacentSum(array, k) {
if (array.length < k)
return null
let runningSum = 0
for (let i = 0; i < k; i++)
runningSum += array[i]
let highestSum = runningSum
for (let j = 0; j < array.length - k; j++) {
function countUnique(iterable) {
if (iterable.length < 1)
return 0
let i = 0
let j = 1
let count = 1
for (; j < iterable.length; j++) {
if (iterable[i] !== iterable[j]) {
function sumZero(array) {
let left = 0
let right = array.length - 1
while (left < right) {
let sum = array[left] + array[right]
if (sum === 0)
return true
function same(a1, a2) {
if (a1.length !== a2.length)
return false
let fc1 = frequencyCounter(a1)
let fc2 = frequencyCounter(a2)
for (let key in fc1)
if (!key in fc2 || fc1[key] !== fc2[key])
return false
@4skinSkywalker
4skinSkywalker / anagrams.js
Last active April 16, 2019 09:50
Check if two strings are anagrams
function anagrams(s1, s2) {
if (s1.length !== s2.length)
return false
let fc1 = frequencyCounter(s1)
let fc2 = frequencyCounter(s2)
for (let key in fc1)
if (!key in fc2 || fc1[key] !== fc2[key])
return false
function frequencyCounter(iterable) {
let fc = {}
for (let item of iterable)
fc[item] = fc[item] + 1 || 1
return fc
}
let string = "hello world"
let frequencyCounter = {
" ": 1
d: 1
e: 1
h: 1
l: 3
o: 2
r: 1
class Percolation {
constructor(UnionFind, SquareGrid) {}
init(gridSize) {
// init union-find
this.uf = new UnionFind(gridSize ** 2 + 2);
// init top virtual node
let top = 0;
for (let i = 1; i <= gridSize; i++) {