[Swift] Summary of how to remove elements from an array (personal note)

I always search many times, so I will summarize it.

Delete first element

var strArray = ["A","B","C","D","E"]

strArray.removeFirst()  //["B", "C", "D", "E"]

Delete last element

var strArray = ["A","B","C","D","E"]

strArray.removeLast()  //["A", "B", "C", "D"]

Delete all elements

var strArray = ["A","B","C","D","E"]

strArray.removeAll()  //[]

Delete the specified element

var strArray = ["A","B","C","D","E"]

strArray.removeAll(where: {$0 == "C"}) // ["A", "B", "D", "E"]

Delete element by specifying index

var strArray = ["A","B","C","D","E"]

strArray.remove(at: 2) //["A", "C", "D", "E"]

Delete the element whose index is unknown (when the previous one cannot be used)

If you want to delete "C", specify something other than "C" with filter.

var strArray = ["A","B","C","D","E"]

strArray = strArray.filter { $0 != "C" } // ["A", "B", "D", "E"]

Recommended Posts

[Swift] Summary of how to remove elements from an array (personal note)
[Swift] How to get the number of elements in an array (super basic)
[Swift5] How to get an array and the complement of arrays
Summary of how to select elements in Selenium
How to output standard from an array with forEach
How to add elements without specifying the length of the array
[java] Summary of how to handle char
Summary of how to write annotation arguments
How to call Swift 5.3 code from Objective-C
Compare the elements of an array (Java)
[Java] [Maven3] Summary of how to use Maven3
Convert an array of strings to numbers
[Ruby] Get unique elements from an array
How to convert an array of Strings to an array of objects with the Stream API
[For Rails beginners] Summary of how to use RSpec (get an overview)
[Java] How to convert one element of a String type array to an Int type
[Ruby] Calculation by extracting elements from an array
Summary of how to create JSF self-made tags
[Java] Summary of how to abbreviate lambda expressions
How to use an array for HashMap keys
Note how to use Swift super basic TableView
[swift5] How to transition from the app to an external site by specifying the URL
Summary of moss when updating from JMockit 1.4 to 1.30
[Swift UI] How to disable ScrollsToTop of ScrollView
How to get an arbitrary digit from a number of 2 or more digits! !!
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
[Ruby] Summary of how to get values from standard input [Paiza skill check measures]
How to request by passing an array to the query with HTTP Client of Ruby
[Swift] How to play songs from your music library
How to use an array for a TreeMap key
How to write Scala from the perspective of Java
How to write to apply gem Pagy (pagination) to an array
How to remove Tiles from TERASOLUNA 5.x blank project
Summary of Java communication API (1) How to use Socket
[Eclipse] Summary of environment settings * Updated from time to time
Summary of Java communication API (3) How to use SocketChannel
How to prevent editTextPreference of android PreferenceFragmentCompat from breaking
Summary of Java communication API (2) How to use HttpUrlConnection
Summary of how to implement default arguments in Java
[Swift] How to get the document ID of Firebase
[Swift 5] Note: Add Core Data to an existing project
[Ruby] How to extract a specific value from an array under multiple conditions [select / each]
How to specify an array in the return value / argument of the method in the CORBA IDL file
How to transition from the [Swift5] app to the iPhone settings screen
[Swift] Table View Cell, how to jump to an external URL
[iOS] [Objective-C] How to update a widget from an Objective-C app
How to change a string in an array to a number in Ruby
[For Ruby beginners] Explain how to freely delete array elements!
How to retrieve the hash value in an array in Ruby
How to store a string from ArrayList to String in Java (Personal)
How to get the longest information from Twitter as of 12/12/2016
How to Burning a Install Disk of Windows from Ubuntu
What is an immutable object? [Explanation of how to make]
How to change BackgroundColor etc. of NavigationBar in Swift UI
Generate Stream from an array of primitive types in Java
[Integration test code] How to select an element from date_select
[Ruby] How to batch convert strings in an array to numbers
Calculate age from birthday with 4 lines of Swift ~ How old are you now? To you who became ~
[Swift] How to use UserDefaults
How to use Swift UIScrollView
[Note] How to write Dockerfile/docker-compose.yml