Skip to content

Instantly share code, notes, and snippets.

View brianscroggins24's full-sized avatar

brianscroggins24

View GitHub Profile
@brianscroggins24
brianscroggins24 / flatten.js
Last active October 25, 2016 18:59
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].
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 October 25, 2016 14:50
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].
/*
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++) {