Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to remove an element from an array in Swift

2 Answers

0 votes
import Foundation

var arr = [1, 2, 3, 4, 5, 6]

arr.remove(at: 2) // Remove the element at index 2

print(arr)



/*
run:

[1, 2, 4, 5, 6]

*/

 



answered Jan 22, 2025 by avibootz
0 votes
import Foundation

var arr = [1, 2, 3, 4, 5, 6]

// Remove the element by value 
if let index = arr.firstIndex(of: 3) {
    arr.remove(at: index)
}

print(arr)


/*
run:

[1, 2, 4, 5, 6]

*/

 



answered Jan 22, 2025 by avibootz

Related questions

3 answers 256 views
1 answer 57 views
1 answer 128 views
128 views asked Jun 15, 2023 by avibootz
3 answers 359 views
1 answer 179 views
...