[Swift5] How to get an array and the complement of arrays


(Consider the case of executing array1 --array2.)

  1. Combine array1 and array2 and convert to Set
  2. Remove the elements of array2 from the result of 1


let array1 = ["a", "b", "c", "d", "e"]
let array2 = ["a", "c", "e"]

let unionSet = Set(array1 + array2)
let diffArray = Array(unionSet.subtracting(array2))
// ["b", "d"]

You can do the same for your own structures.

struct Food: Hashable {
    //Hashable protocol methods
    static func == (lhs: Food, rhs: Food) -> Bool {
        return (lhs.name == rhs.name) && (lhs.price == rhs.price)
    let name: String
    let price: Int

let food1 = Food(name: "ramen", price: 800)
let food2 = Food(name: "Udon", price: 700)
let food3 = Food(name: "Soba", price: 600)

let myFavoriteFood = [food1, food2, food3]
let yourFavoriteFood = [food1]

let unionSet = Set(myFavoriteFood + yourFavoriteFood)
let diffArray = Array(unionSet.subtracting(yourFavoriteFood))
print(diffArray[0], diffArray[1])
// Food(name: "Udon", price: 700) Food(name: "Soba", price: 600)

Recommended Posts

[Swift5] How to get an array and the complement of arrays
[Swift] How to get the number of elements in an array (super basic)
[Swift] How to get the document ID of Firebase
[Rails] How to get the URL of the transition source and redirect
[Swift UI] How to get the startup status of the application [iOS]
How to get the length of an audio file in java
[Swift5] How to analyze complex JSON and get the index of the element that satisfies the condition
How to get today's day of the week
[Java] How to get the authority of the folder
[Swift] Summary of how to remove elements from an array (personal note)
How to convert an array of Strings to an array of objects with the Stream API
Get the type of an array element to determine if it is an array
[Java] How to get the URL of the transition source
How to get the ID of a user authenticated with Firebase in Swift
Convert the array of errors.full_messages to characters and output
[Java] Program example to get the maximum and minimum values from an array
[Java] How to get the maximum value of HashMap
[Android] How to get the setting language of the terminal
[Rails] How to get the contents of strong parameters
[Ruby] How to get the tens place and the ones place
How to request by passing an array to the query with HTTP Client of Ruby
An unsupported Java version How to get rid of errors
How to retrieve the hash value in an array in Ruby
How to get the longest information from Twitter as of 12/12/2016
How to add elements without specifying the length of the array
How to check the extension and size of uploaded files
[jsoup] How to get the full amount of a document
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
How to specify an array in the return value / argument of the method in the CORBA IDL file
How to set the IP address and host name of CentOS8
How to output the value when there is an array in the array
How to get the contents of Map using for statement Memorandum
The story of toString () starting with passing an array to System.out.println
Method to add the number of years and get the end of the month
How to get the id of PRIMAY KEY auto_incremented in MyBatis
[Java] How to convert from String to Path type and get the path
Compare the elements of an array (Java)
How to determine the number of parallels
[Java] How to get the current directory
[Swift] How to implement the countdown function
[Swift] Get the height of Safe Area
Convert an array of strings to numbers
How to sort the List of SelectItem
How to find the tens and ones
How to get the date in java
[Java] How to get the current date and time and specify the display format
How to change the maximum and maximum number of POST data in Spark
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
[Java improvement case] How to reach the limit of self-study and beyond
[For Rails beginners] Summary of how to use RSpec (get an overview)
[Swift] How to set an image in the background without using UIImageView.
[swift5] How to change the color of TabBar or the color of item of TabBar with code
How to create your own annotation in Java and get the value
[Swift] How to reflect the array changed at the transition destination to the transition source and display it on Label etc.
JDBC promises and examples of how to write
Customize how to divide the contents of Recyclerview
[Java] How to get and output standard input
[Swift] How to implement the LINE login function
How to use an array for HashMap keys
[swift5] How to implement the Twitter share function