0

I m kind of new in functional programming If I have 2 arrays of strings, is there a way using map, filter and zip to return the matching values ? I know a dirty way to do it, by iterating in each array and comapring the values to find matches but there must be a clean way to do it.

so var example i have the following arrays :

array1 = ["max","julie","helmut","igor"] array2 = ["sophie","igor"]

and I would like to retrieve "igor" as a value

another thing, if there s more than one match, witch in my case is not possible, I would like to know how to return a string of matches (just for my own understanding)

Thanks for your help :)

1
  • 2
    What here is functional programming? Commented Mar 4, 2016 at 3:55

5 Answers 5

3

Something I whipped up quick. Should work for anything adhering to Equatable (such as a String):

func getMatches<T: Equatable>(firstArray: [T], secondArray: [T]) -> [T] {
    return firstArray.filter({secondArray.contains($0)})
}

Alternately, this is a generic version of Kevin's answer

func getMatches<T: Equatable>(firstArray: [T], secondArray: [T]) -> Set<T> {
    return Set(firstArray).intersect(secondArray)
}
Sign up to request clarification or add additional context in comments.

Comments

1

Set has a intersect method which returns the values that exist in the set and any SequenceType. I'm not sure if this is "functional" but it's certainly not "dirty".

let array1 = ["max","julie","helmut","igor"]
let array2 = ["sophie","igor","helmut"]

let common = Set(array1).intersect(array2)
print(common) //["igor", "helmut"]

4 Comments

That s why I love internet, I love you also
This will have runtime of O(len(array1)) for constructing the set + O(len(array2)) for intersect(). This will be much faster than O(len(array1)) for filter + O(len(array2) * len(array2)) for contains. This is a great trade if you can afford disregarding duplicates and can spare some extra memory.
nice, I was just wondering what was faster and testing both approaches, my arrays are HUGE, thanks for clearing this out for me
Added a generic version of this to my answer
0

This will work for your purposes:

let array1 = ["max","julie","helmut","igor"]
let array2 = ["sophie","igor"]

let arrayOfMatches = array1.filter { array2.contains($0) }
print(arrayOfMatches)

Let me know if you need anything more explained.
This is the shortest way to write this kind of statement.
Essentially filter needs to return a boolean that determines whether or not to keep the value over which it is iterating.

1 Comment

tks :) it makes sense
0

Try this.

let both = array1.filter{ array2.contains($0) }

enter image description here

Comments

0

Alternative PEEJWEEJ's answer.

If you return value [T] instead of Set<T>, try this. (T must conform to hashable)

func getMatches<T: Hashable>(firstArray: [T], secondArray: [T]) -> [T] {
    return Array(Set(firstArray).intersect(secondArray))
}

2 Comments

On a more serious note, will the returned array have an expected ordering? jc.
Sorry...! I fixed typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.