func anyCommonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> Bool where T.Iterator.Element: Equatable, T.Iterator.Element == U.Iterator.Element { for lhsItem in lhs { for rhsItem in rhs { if lhsItem == rhsItem { return true } } } return false } anyCommonElements([1, 2, 3], [3])在 這個帖子里摘出來的
func anyCommonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> T where T.Iterator.Element: Equatable, T.Iterator.Element == U.Iterator.Element { var temp = T() for lhsItem in lhs { for rhsItem in rhs { if lhsItem == rhsItem { temp.appand(lhsItem) } } } return temp } anyCommonElements([1, 2, 3], [3])