Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove common elements from one array by comparing it with another array [duplicate]

Tags:

arrays

swift

I have a class

class CardImage {
var imageName: String?
var image : UIImage?
var is_saved = false
}

I have two arrays

var imagesToBeDeleted = [String]()
var cardImages = [CardImage]()

I want to delete elements from cardImages that has property imageName similar to the elements present in imagesToBeDeleted.

EXTRA DETAIL: from the names of both arrays you can figure out that imagesToBeDeleted will be subset of imageName property of cardImages array.

like image 886
mobs_boss Avatar asked Mar 14 '26 05:03

mobs_boss


1 Answers

You just have to use filter on cardImages, check if the name of the current card is present in the imagesToBeDeleted array or not and only keep the elements of cardImages that are not present in the other array.

cardImages = cardImages.filter{!imagesToBeDeleted.contains($0.imageName)}

like image 195
Dávid Pásztor Avatar answered Mar 15 '26 19:03

Dávid Pásztor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!