Skip to content

Instantly share code, notes, and snippets.

@NickHung1982
Last active September 7, 2017 04:00
Show Gist options
  • Save NickHung1982/b47b91d4ecf918665a0675a81cf1b963 to your computer and use it in GitHub Desktop.
Save NickHung1982/b47b91d4ecf918665a0675a81cf1b963 to your computer and use it in GitHub Desktop.
flatten nested
var returnList = [Int]()
public func separateArray(_ ar:[Any]) {
for li in ar {
if li is Int {
returnList.append(li as! Int)
}else{
for element in li as! Array<Any> {
if element is Int {
returnList.append(element as! Int)
}else{
separateArray(element as! Array<Any>)
}
}
}
}
}
separateArray([[1,[1,3]],2,[1,1]])
print(returnList) //"[1, 1, 3, 2, 1, 1]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment