Array Filter in Swift
Level 3101 keystrokes
Uses filter with a trailing closure to keep only the even numbers from 1 to 10 and prints [2, 4, 6, 8, 10].
Swift code
// The numbers 1 to 101let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]// filter keeps the elements for which the closure returns true// $0 is the current element; $0 % 2 == 0 is true for even numbers2let evens = numbers.filter { $0 % 2 == 0 }// Prints [2, 4, 6, 8, 10]3print(evens)Output
[2, 4, 6, 8, 10]
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Swift code and how often they appear.
,× 9=× 4.× 1(× 1)× 1[× 1]× 1{× 1}× 1%× 1