美文网首页
练习: 修改 anyCommonElements(_:_:) 函

练习: 修改 anyCommonElements(_:_:) 函

作者: Boy_iOS | 来源:发表于2016-12-18 19:59 被阅读45次
     func anyCommonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> [T.Iterator.Element]
            where T.Iterator.Element: Equatable, T.Iterator.Element == U.Iterator.Element {
                
                var list: [T.Iterator.Element] = []
                
                print(type(of: list))
                
                for lhsItem in lhs {
                    
                    for rhsItem in rhs {
                        
                        if lhsItem == rhsItem {
                            
                            list.append(lhsItem)
                            return list
                        }
                    }
                }
                
                return list
        }
     
        print(anyCommonElements([1, 2, 3], [3]))
        
        print(anyCommonElements(["guo", "teng", "zhao"], ["zhao"]))
    

    相关文章

      网友评论

          本文标题:练习: 修改 anyCommonElements(_:_:) 函

          本文链接:https://www.haomeiwen.com/subject/mtdzmttx.html