1

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", "]"]
David Sanford
  • 735
  • 12
  • 26
  • 5
    Why do your two arrays only have one value each and why is each of those two values a string containing comma separated numbers? The arrays seem pointless if all you have is a single string in each. – rmaddy Oct 26 '17 at 23:37
  • @maddy, you caught me. I am new to this and inherited this code from someone I hired. I am now trying to clean up his code. My app is published and has about 60 classes and when I tried to change this to be Integers and not strings, but failed miserably. Aside from the poor direction I am faced, any suggestions on getting exact matches? – David Sanford Oct 26 '17 at 23:50
  • The above is an example. Some arrays will have thousands of numbers – David Sanford Oct 26 '17 at 23:50
  • It's not really clear what you're going for, but if you're trying to match the numbers that are in the strings that are in the arrays, you need to use `componentsSeparated(by:)` on the strings first. – jscs Oct 26 '17 at 23:59
  • Let me explain the two. My app is about species in the oceans. The two arrays are finding say "sharks", so I will have 60 species ID of sharks. Then I want to see which sharks are in the Red Sea. The Red Sea ID will have maybe 1000 total species. So, I want to extract of these 1000 species, those that match the shark id and display this to the end use. Right now, what I have works, but it is getting more species than it should as it is not extracting exact matches. Does this help? – David Sanford Oct 27 '17 at 00:02
  • 1
    The first thing you need to do is to replace your string of comma separated numbers with an array of actual numbers. An array with a single string is pointless. – rmaddy Oct 27 '17 at 00:07
  • Here is an unfiltered set of data from one array: - 0 : "695" - 1 : "696" - 2 : "697" - 3 : "698" - 4 : "1070" - 5 : "1071" - 6 : "1072" - 7 : "1073" - 8 : "1074" - 9 : "1075" - 10 : "1076" - 11 : "1368" - 12 : "1409" - 13 : "1526" - 14 : "1628" - 15 : "1781" - 16 : "2067" - 17 : "2068" - 18 : "2106" - 19 : "2107" - 20 : "2159" – David Sanford Oct 27 '17 at 00:13
  • then, let sharksFinal = newArray.joined(separator: ", ") creates the above format. I know this is what needs to change, but way above my skill level – David Sanford Oct 27 '17 at 00:14
  • Let me play with this some and try to convert to Int again. I think i need to make this happen first – David Sanford Oct 27 '17 at 00:18

1 Answers1

4

I ran your exact code in swift playground and get newArray equaling an empty array. This is because the firstArray contains only one object, the String

"9,54,59,60,66,362,372,399,400,411,428"

The commas do not break the string up into smaller strings or integers. All the characters inside the quotation marks are part of the string including commas.

The secondArray also only contains one string

"9,40,62,399"

Filtering the firstArray tests whether the string "9,40,62,399" contains the string "9,54,59,60,66,362,372,399,400,411,428", which is false, and so the newArray is empty.

If you remove the quotes at the beginning and end inside of the arrays then all the numbers are Ints and the same function returns newArray with only matches [9, 399].

For Int

let firstArray = [9,54,59,60,66,362,372,399,400,411,428]
let secondArray = [9,40,62,399]

let newArray  = firstArray.filter { (string) -> Bool in
    return secondArray.contains(string)
}
// Returns [9, 399]

For Strings

let firstArray = ["9","54","59","60","66","362","372","399","400","411","428"]
let secondArray = ["9","40","62","399"]

let newArray  = firstArray.filter { (string) -> Bool in
    return secondArray.contains(string)
}
// Returns ["9", "399"]
Yarn
  • 238
  • 1
  • 12
  • Please note the above update as it is not working. Not sure where the difference between the app and the playground version – David Sanford Oct 27 '17 at 04:19
  • Your code was spot on. I found that my issue was I needed to separate by components first, then run your code. I have selected you as the answer. Thanks – David Sanford Oct 27 '17 at 22:48