Created
May 20, 2016 20:58
-
-
Save shuklaneerajdev/9fb5af4adb875b413d3bc9e6cc312859 to your computer and use it in GitHub Desktop.
This file contains 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(input) | |
result = Array.new | |
input.each do |element| | |
if element && element.instance_of?(Array) | |
# if the current element is an array then recursively flatten that | |
# array and add that array to the result | |
result = result+flatten(element) | |
else | |
# else this is just a regular element and add the element to result | |
result << element | |
end | |
end | |
return result | |
end | |
input = [[1,2,[3]],4] | |
print flatten(input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment