We can remove all elements of an array that satisfy a given predicate in Swift by using the removeAll(where:)
method.
arr.removeAll(where:)
where:
: This is a criteria, condition, or predicate. Once this is met, the element is removed.
The value returned is an array with the elements that meet the criteria to be removed.
// create arraysvar languages = ["Java", "Python", "Ruby", "Perl", "Swift"]var numbers = [1, 2, 3, 4]// remove based on criterialanguages.removeAll {$0.count > 4}// remove strings whose length// is greater than 4numbers.removeAll{$0 % 2 != 0}// remove numbers that// are not even numbers// print modified arraysprint(languages)print(numbers)
languages
and numbers
.languages
.numbers
.