Created
August 31, 2017 17:02
-
-
Save pulkit21/d10b6a7f63530f81865363f2bae944d5 to your computer and use it in GitHub Desktop.
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]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flatten_array(array) | |
array.each_with_object([]) do |element, flat| | |
flat.push *(element.is_a?(Array) ? flatten_array(element) : element) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment