I have two arrays. Example:
firstArray = ["9,54,59,60,66,362,372,399,400,411,428"]
secondArray - ["9,40,62,399"]
let newArray = firstArray.filter { (string) -> Bool in
return secondArray.contains(string)
However, my newArray
is returning 4 results, when only two match as 40
and 62
are "matching" 400
and 362
in error.
I found this question, which helped me get closer, but does not solve the exact match error. How to get list of common elements of 2 array in swift answered by RAJAMOHAN-S
How to set this up to get only exact matches?
UPDATE: Something is not working correct.
As per the below, I have the following code:
subGroupArrayResult.insert("[", at: subGroupArrayResult.startIndex)
subGroupArrayResult.insert("]", at: subGroupArrayResult.endIndex)
arraySpeciesIDResult.insert("[", at: arraySpeciesIDResult.startIndex)
arraySpeciesIDResult.insert("]", at: arraySpeciesIDResult.endIndex)
let newArray = subGroupArrayResult.filter { (string) -> Bool in
return arraySpeciesIDResult.contains(string)
}
print("subGroupArrayResult is \(subGroupArrayResult)")
print("arraySpeciesIDResult is \(arraySpeciesIDResult)")
print("new Array is \(newArray)")
However, here are the print results:
subGroupArrayResult is [695,696,697,698,1070,1071,1072,1073,1074,1075,1076,1368,1409,1526,1628,1781,2067,2068,2106,2107,2159]
arraySpeciesIDResult is [699,836,1266,1281,1426,1447,1474,1784,699,836,1266,1314,1426,1447,1474,1764,1784,1905,699,836,1266,1314,1426,1447,1474,1784,699,836,1266,1426,1447,1474,1784,699,836,1266,1426,1447,1474,1784,699,836,1426,1447,1474,1784]
new Array is ["[", "6", "9", "5", ",", "6", "9", "6", ",", "6", "9", "7", ",", "6", "9", "8", ",", "1", "0", "7", "0", ",", "1", "0", "7", "1", ",", "1", "0", "7", "2", ",", "1", "0", "7", "3", ",", "1", "0", "7", "4", ",", "1", "0", "7", "5", ",", "1", "0", "7", "6", ",", "1", "3", "6", "8", ",", "1", "4", "0", "9", ",", "1", "5", "2", "6", ",", "1", "6", "2", "8", ",", "1", "7", "8", "1", ",", "2", "0", "6", "7", ",", "2", "0", "6", "8", ",", "2", "1", "0", "6", ",", "2", "1", "0", "7", ",", "2", "1", "5", "9", "]"]