How to filter out all elements that do not equal to specific string in array with Swift

2 Answers

0 votes
let arr = ["swift", "java", "swift", "c++", "python", "swift"]

let filter_arr = arr.filter { (s) -> Bool in s == "swift" }

print(arr)
print(filter_arr)



/*
run:

["swift", "java", "swift", "c++", "python", "swift"]
["swift", "swift", "swift"]

*/

 



answered Oct 15, 2020 by avibootz
0 votes
var arr = ["swift", "java", "swift", "c++", "python", "swift"]
print(arr)

arr = arr.filter { (s) -> Bool in s == "swift" }
print(arr)




/*
run:

["swift", "java", "swift", "c++", "python", "swift"]
["swift", "swift", "swift"]

*/

 



answered Oct 15, 2020 by avibootz
...