func inArray(_ a1: [String], _ a2: [String]) -> [String] {
var array: [String] = []
//see if word exists within word
for string in a1 {
for string2 in a2 {
if string2.range(of:string) != nil {
array.append(string)
}
}
}
//make array unique, remove duplicates
let unique = Array(Set(array))
//sort in alphabetical order
let sortedArray = unique.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedAscending }
//return answer
return sortedArray
}
print(inArray(["tarp", "mice", "bull"], ["lively", "alive", "harp", "sharp", "armstrong"]))
OR
func inArray(_ a1: [String], _ a2: [String]) -> [String] {
return Set(a1.filter { s1 in a2.contains { $0.contains(s1) } }).sorted()
}