Skip to content

Instantly share code, notes, and snippets.

Avatar

brianscroggins24

View GitHub Profile
@brianscroggins24
brianscroggins24 / flatten.js
Last active Oct 25, 2016
Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
View flatten.js
let array = [[1,2,[3],4],[5, 6,[7]]];
let newArray = flatten(array);
function flatten(array) {
if (!array || !array.length) return [];
let stack = [],
indexes = [],
currentIdx = 0,
results = [];
@brianscroggins24
brianscroggins24 / flattenRecursive.js
Last active Oct 25, 2016
Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
View flattenRecursive.js
/*
Example of a flatten array method using recursion. If the original array is
large do NOT use this funtion. Too much memory is used off the stack.
*/
let array = [[1,2,[3]],4];
let newArray = flattenRecursive(array);
function flattenRecursive(array, results = []) {
for (let i = 0; i < array.length; i++) {