How to remove duplicates from int array in Swift

1 Answer

0 votes
func removeDuplicates(values: [Int]) -> [Int] {
    let uniques = Set<Int>(values)
    let arr = Array<Int>(uniques)
    
    return arr
}

var arr: [Int] = [1, 5, 1, 1, 6, 7, 7, 5, 8, 8, 8, 8]
print(arr)

arr = removeDuplicates(values: arr)
print(arr)




/*
run:

[1, 5, 1, 1, 6, 7, 7, 5, 8, 8, 8, 8]
[7, 8, 5, 1, 6]

*/

 



answered Oct 15, 2020 by avibootz

Related questions

...