Skip to content

Instantly share code, notes, and snippets.

@ashalva
Last active May 9, 2018 06:40
Show Gist options
  • Save ashalva/bd64a9bff2df3c92bd6ffbc97549a802 to your computer and use it in GitHub Desktop.
Save ashalva/bd64a9bff2df3c92bd6ffbc97549a802 to your computer and use it in GitHub Desktop.
Returning all duplicates from the array in swift.
//Duplicate
func returnDuplicates(arr: [Int]) -> [Int] {
var dict: [Int: Bool] = [:]
var duplicates: [Int] = []
for i in arr {
if dict.keys.contains(i) {
duplicates.append(i)
} else {
dict[i] = true
}
}
return duplicates
}
returnDuplicates(arr: [1,2,3,5,1,3,2,7]) //result [1, 3, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment