user6032625 user6032625 - 7 months ago 15
Swift Question

How do I find the position of a certain value given a second array?

I am comparing two arrays, and I need to figure out at which position the duplicates occur. I have to map the array, but am unsure on how to do that. In the end I want to have a filtered array. 1 indicates there is a duplicate, and "" indicates there is no duplicate.

var Strarr = ["1", "2", "3", "4"]
var importarr = ["1", "3"]

filteredArray = ["1", "", "1", ""]

Answer
let result = Strarr.map { importarr.contains($0) ? "1" : ""}

print(result) // ["1", "", "1", ""]

A bit shorter.