Last active
December 15, 2017 05:53
-
-
Save neerajkumar/84844b96911d8a611be2bbdb20c30ae7 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