johny johny - 9 months ago 61
Swift Question

Swift 3 - check, how many identical elements array contents

I have an [Int] array and I need to check for all elements, that don't have identical value with some another element. Those non- identical elements I would like to insert to a new array.

var spojeni1 = [1, 1, 2, 3, 4, 4] // Here it is values 2,3
var NewArray = [Int]()
for i in spojeni1 {
if { // the value hasn't another identical value in the array
NewArray.append(i)
}
}


Hope it is clear, thank you

Answer Source

This can be done in a single line:

let numbers = [1, 1, 2, 3, 4, 4] // Here it is values 2,3

let uniques = Set(numbers).filter{ (n) in numbers.filter{$0==n}.count == 1 }