-1

I got error

Anonymous closure argument not contained in a closure

I have a array as shown below and I want to remove the duplicate element to get a new array without any duplicacy , My code of swift is :-

let oldArray = [1,2,3,4,5,6,7,8,91,2,3,6]
var newArray = oldArray.map( $0 != $1)
print(newArray) 

Thanks in advance

Dharmesh Kheni
  • 69,532
  • 33
  • 158
  • 163
piyush ranjan
  • 371
  • 4
  • 14

1 Answers1

1

You can try

let newArray = Array(Set(oldArray))

OR

let oldArray = [1,2,3,4,5,6,7,8,91,2,3,6]
var newArray = [Int]()
oldArray.forEach {
    if !newArray.contains($0) {
        newArray.append($0)
    } 
}
print(newArray)
Sh_Khan
  • 93,445
  • 7
  • 57
  • 76
  • This would be extremely inefficient for larger collections. Better to use a set and filter only the objects that are actually inserted – Leo Dabus Sep 03 '18 at 17:37
  • @LeoDabus i added the other way to clarify that to the op , he has to use option #1 – Sh_Khan Sep 03 '18 at 17:41