Array1 = [Washington, Franklin, Florida, Alaska, California, Georgia]
Array1 = [Washington,Franklin,Florida,Alaska,California,Georgia]
Array2 = [California, Washington, Georgia]
Array2 = [加利福尼亚州,华盛顿,格鲁吉亚]
I want to get
我想得到
Array1 = [Washington, California, Georgia]
Array1 = [华盛顿,加利福尼亚,格鲁吉亚]
4 个解决方案
#1
3
var array1 = ["Washington", "Franklin", "Florida", "Alaska", "California", "Georgia"]
var array2 = ["California", "Washington", "Georgia"]
let filterArray = array1.filter {
array2.contains($0)
}
The filterArray
is what you want
filterArray就是你想要的
#2
2
You can use filter for achieving desired output
您可以使用过滤器来实现所需的输出
Using filter:
使用过滤器:
let array1 = ["Washington", "Franklin", "Florida", "Alaska", "California", "Georgia"]
let array2 = ["California", "Washington", "Georgia"]
let aryCommonElements = array1.filter { array2.contains($0)}
Output:
输出:
["Washington", "California", "Georgia"]
Hope this will help you :)
希望对你有帮助 :)
#3
2
try this
尝试这个
let filteredArray = array1.filter({Array2.contains($0)})
#4
1
If you want to retrieve only common elements than use:
如果您只想检索常用元素而不是使用:
In Swift 3
在Swift 3中
let Array1 = ["Washington", "Franklin", "Florida", "Alaska", "California", "Georgia"]
let Array2 = ["California", "Washington", "Georgia"]
let common = GetCommonElements(lhs: Array1, rhs: Array2)
print(common)
func GetCommonElements <T, U> (lhs: T, rhs: U) -> [T.Iterator.Element] where T: Sequence, U: Sequence, T.Iterator.Element: Equatable, T.Iterator.Element == U.Iterator.Element {
var returnArray:[T.Iterator.Element] = []
for lhsItem in lhs {
for rhsItem in rhs {
if lhsItem == rhsItem {
returnArray.append(lhsItem)
}
}
}
return returnArray
}
#1
3
var array1 = ["Washington", "Franklin", "Florida", "Alaska", "California", "Georgia"]
var array2 = ["California", "Washington", "Georgia"]
let filterArray = array1.filter {
array2.contains($0)
}
The filterArray
is what you want
filterArray就是你想要的
#2
2
You can use filter for achieving desired output
您可以使用过滤器来实现所需的输出
Using filter:
使用过滤器:
let array1 = ["Washington", "Franklin", "Florida", "Alaska", "California", "Georgia"]
let array2 = ["California", "Washington", "Georgia"]
let aryCommonElements = array1.filter { array2.contains($0)}
Output:
输出:
["Washington", "California", "Georgia"]
Hope this will help you :)
希望对你有帮助 :)
#3
2
try this
尝试这个
let filteredArray = array1.filter({Array2.contains($0)})
#4
1
If you want to retrieve only common elements than use:
如果您只想检索常用元素而不是使用:
In Swift 3
在Swift 3中
let Array1 = ["Washington", "Franklin", "Florida", "Alaska", "California", "Georgia"]
let Array2 = ["California", "Washington", "Georgia"]
let common = GetCommonElements(lhs: Array1, rhs: Array2)
print(common)
func GetCommonElements <T, U> (lhs: T, rhs: U) -> [T.Iterator.Element] where T: Sequence, U: Sequence, T.Iterator.Element: Equatable, T.Iterator.Element == U.Iterator.Element {
var returnArray:[T.Iterator.Element] = []
for lhsItem in lhs {
for rhsItem in rhs {
if lhsItem == rhsItem {
returnArray.append(lhsItem)
}
}
}
return returnArray
}