Last active
May 9, 2018 06:40
-
-
Save ashalva/bd64a9bff2df3c92bd6ffbc97549a802 to your computer and use it in GitHub Desktop.
Returning all duplicates from the array in swift.
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
//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