🌐
GeeksforGeeks
geeksforgeeks.org › swift › swift-difference-between-sets-and-arrays
Swift - Difference Between Sets and Arrays - GeeksforGeeks
November 4, 2022 - // Swift program to illustrate array // Creating and initializing an Array of String Values var arr = [ "GeeksforGeeks", "GFG", "Swift Programs" ] // Display the array print(arr) ... Set is a data structure where no duplicate elements are present. ...
🌐
Medium
medium.com › swlh › swift-array-vs-set-f2edf9e8da1d
Swift Array vs. Set: A Head-To-Head Comparison in Performance | by Yong Cui | The Startup | Medium
January 19, 2020 - As shown below, Array is a collection of ordered values, Set is a collection of unique values, while Dictionary is an unordered collection of key-value pairs. ... They are all generic data types as discussed in one of my previous Medium articles.
🌐
Hacking with Swift
hackingwithswift.com › sixty › 2 › 4 › arrays-vs-sets-vs-tuples
Arrays vs sets vs tuples - a free Hacking with Swift tutorial
May 28, 2019 - let address = (house: 555, street: ... "astronaut", "azalea"]) If you need a collection of values that can contain duplicates, or the order of your items matters, you should use an array: let pythons = ["Eric", "Graham", ...
🌐
SwiftLee
avanderlee.com › swiftlee › swift › array vs set: fundamentals in swift explained
Array vs Set: Fundamentals in Swift explained - SwiftLee
January 26, 2024 - One practical email every Tuesday. Unsubscribe anytime. A big difference between Sets and Arrays is the uniqueness of elements. An Array can contain the same value twice while a Set will never contain duplicates.
🌐
Cocoacasts
cocoacasts.com › swift-fundamentals-arrays-and-sets
Arrays and Sets - Cocoacasts
January 27, 2018 - For some tasks, sets are more performant than arrays. That's something to keep in mind. Collection types are the bread and butter of the Swift developer, which means it pays to invest some time in understanding how they work and, more importantly, when to use which collection type for a particular task.
🌐
Hacking with Swift
hackingwithswift.com › example-code › language › when-to-use-a-set-rather-than-an-array
When to use a set rather than an array - free Swift example code and tips
November 28, 2019 - You don’t care about the order of the items in the set. You don’t need to use APIs that require arrays. You’re storing Hashable types, either your own or one of Swift’s built-in types likes strings and integers.
🌐
Donny Wals
donnywals.com › how-to-decide-between-a-set-and-array-in-swift
How to decide between a Set and Array in Swift? – Donny Wals
October 2, 2024 - Arrays are meant to hold data that is ordered and this data doesn’t have to be unique · A Set in Swift holds a single type of object, just like Array does.
Top answer
1 of 4
34

Here are the practical differences between the different types:

Arrays are effectively ordered lists and are used to store lists of information in cases where order is important.

For example, posts in a social network app being displayed in a tableView may be stored in an array.

Sets are different in the sense that order does not matter and these will be used in cases where order does not matter.

Sets are especially useful when you need to ensure that an item only appears once in the set.

Dictionaries are used to store key, value pairs and are used when you want to easily find a value using a key, just like in a dictionary.

For example, you could store a list of items and links to more information about these items in a dictionary.

Hope this helps :)

(For more information and to find Apple's own definitions, check out Apple's guides at https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html)

2 of 4
15

Detailed documentation can be found here on Apple's guide. Below are some quick definations extracted from there:

Array

An array stores values of the same type in an ordered list. The same value can appear in an array multiple times at different positions.

Set

A set stores distinct values of the same type in a collection with no defined ordering. You can use a set instead of an array when the order of items is not important, or when you need to ensure that an item only appears once.

Dictionary

A dictionary stores associations between keys of the same type and values of the same type in a collection with no defined ordering. Each value is associated with a unique key, which acts as an identifier for that value within the dictionary. Unlike items in an array, items in a dictionary do not have a specified order. You use a dictionary when you need to look up values based on their identifier, in much the same way that a real-world dictionary is used to look up the definition for a particular word.

🌐
SerialCoder
serialcoder.dev › text-tutorials › swift-tutorials › arrays-vs-sets-in-swift
Arrays VS Sets In Swift – SerialCoder.dev
Performance-wise, Set is always a better choice because of its implementation details and the way it stores its elements in memory, which are required to conform to Hashable protocol. Since it has to do with hash values internally, it provides a constant time for operations on elements (O(1)). ...
Find elsewhere
🌐
Better Programming
betterprogramming.pub › array-vs-set-fundamentals-in-swift-explained-9c78f3552e52
Array vs Set: Fundamentals in Swift explained
August 7, 2019 - When we compare the initialization of Arrays and Sets we can see that they’re pretty similar: let arrayOfBlogCategories: [String] = ["Swift", "Debugging", "Xcode", "Workflow", "Optimization"] let setOfBlogCategories: Set<String> = ["Swift", "Debugging", "Xcode", "Workflow", "Optimization"]
🌐
Swift.org
docs.swift.org › swift-book › LanguageGuide › CollectionTypes.html
The Swift Programming Language: Redirect
September 22, 2022 - This content has moved; redirecting to the new location · This page requires JavaScript. Please turn on JavaScript and refresh the page
🌐
TutorialsPoint
tutorialspoint.com › swift › swift_sets.htm
Swift - Sets
Python TechnologiesDatabasesComputer ... Tutorials View All Categories ... Swift sets are used to store distinct values of the same types but they dont have definite ordering as arrays have....
🌐
Medium
vikramios.medium.com › exploring-swift-collection-types-arrays-sets-and-dictionaries-3dda14348298
Exploring Swift Collection Types: Arrays, Sets, and Dictionaries | by Vikram Kumar | Medium
October 28, 2023 - Arrays are ordered collections of values that can be accessed by an index. They are used to store an ordered sequence of elements of the same type. Arrays in Swift are versatile and can grow or shrink dynamically.
Top answer
1 of 1
2

For just 1000 objects it shouldn't really matter.

You may try the following test (for 1 million objects):

struct Foo: Hashable {
    let id: Int
    let name: String
}

let fooArr: [Foo] = (1...1_000_000).map { Foo(id: $0, name: "hkafghhkjdsgfajhkgfkjhasdf") }
let fooSet: Set<Foo> = Set(fooArr)

let start1 = CFAbsoluteTimeGetCurrent()
let filteredArr = fooArr.filter { $0.id.isMultiple(of: 2) }
let diff1 = CFAbsoluteTimeGetCurrent() - start1
print("Array: \(diff1) seconds")
// prints "Array: 4.004199028015137 seconds"

let start2 = CFAbsoluteTimeGetCurrent()
let filteredSetToArr = Array(fooSet.filter { $0.id.isMultiple(of: 2) })
let diff2 = CFAbsoluteTimeGetCurrent() - start2
print("Set: \(diff2) seconds")
// prints "Set: 4.139569997787476 seconds"

Again there isn't much of a difference. The actual values may differ but the relative difference should be similar for every try.

I used -Ofast optimisation level.


You can even make more detailed tests:

let fooArr: [Foo] = (1...1_000_000).map { Foo(id: $0, name: "hkafghhkjdsgfajhkgfkjhasdf") }
let fooSet: Set<Foo> = Set(fooArr)

let start1 = CFAbsoluteTimeGetCurrent()
let filteredArr = fooArr.filter { $0.id.isMultiple(of: 2) }
let diff1 = CFAbsoluteTimeGetCurrent() - start1
print("Array filtering: \(diff1) seconds")

let start2 = CFAbsoluteTimeGetCurrent()
let filteredSet = Array(fooSet.filter { $0.id.isMultiple(of: 2) })
let diff2 = CFAbsoluteTimeGetCurrent() - start2
print("Set filtering: \(diff2) seconds")

let start3 = CFAbsoluteTimeGetCurrent()
let filteredSetToArr = Array(filteredSet)
let diff3 = CFAbsoluteTimeGetCurrent() - start3
print("Set to Array: \(diff3) seconds")

print("Set total: \(diff2 + diff3) seconds")

and the result will be:

Array filtering: 1.5497009754180908 seconds
Set filtering: 1.2836240530014038 seconds
Set to Array: 1.895427703857422e-05 seconds
Set total: 1.2836430072784424 seconds

Note that filtering is way more expensive than conversion from Set to Array.

🌐
Medium
medium.com › @rashadsh › array-vs-set-in-swift-exploring-the-differences-and-best-use-cases-d04c00da5b2f
Array vs Set in Swift: Exploring the Differences and Best Use Cases | by Rashad Shirizada | Medium
July 11, 2023 - In this blog post, we will dive into the distinctions between arrays and sets, explore their use cases, and provide code examples to illustrate their usage. Array: An array is an ordered collection of elements of the same type. It allows duplicate values and maintains the order of elements based on their indices. Arrays are defined using square brackets and can be mutable or immutable.
🌐
Swift by Sundell
swiftbysundell.com › articles › the-power-of-sets-in-swift
The power of sets in Swift | Swift by Sundell
March 4, 2018 - Swift by Sundell is brought to ... line of code. Try it today. One of the key characteristics of a Set is that it stores its members based on hash value rather than by index (like Array does)....
🌐
YouTube
youtube.com › watch
Swift Basics: Array vs. Set - YouTube
Go to https://squarespace.com/seanallen to save 10% off your first purchase of a website or domain using code SEANALLEN. Most iOS developers instinctively re...
Published: March 31, 2023
🌐
TutorialsPoint
tutorialspoint.com › swift-program-to-convert-set-into-array
Swift Program to Convert Array to Set
April 6, 2023 - An array is used to store elements of the same data type in an order whereas a set is used to store distinct elements of the same data type without any definite order. To convert Array into set we use Set() initialiser.
🌐
Medium
medium.com › macoclock › swift-collection-types-array-vs-set-c048b0abbdd9
Swift Collection Types: Array vs Set | by Batikan Sosun | Mac O’Clock | Medium
June 22, 2020 - A Set is unordered, and each element can only appear once in a set. While an Array can contain duplicate elements, each value contained in a set is unique.