'How to check if element of an array is in another array?

I have two Array holding the instances of class MYItems. FavoriteItems holds the favorite items from the array Items

var Items :[MYItems] = []
var favoriteItems = [MYItems]()

Now i want to check if the Items array contains the favoriteItems in the . How can i acheive this? i tried using the below Find method

if let index = find(favoriteItems, Items[indexPath.row]){

             println("found item")
   }else{
             println("doesnotcontains item")
        }

I tried using the below but that doesnot help too..

 if contains(favoriteItems, Items[indexPath.row]){

            println("found item")

        }else{

         println("doesnot contain item")

        }

But it always goes to the else block? Why is this happening when i contain one of the items in array for sure.



Solution 1:[1]

Convert the arrays to a Set and then use isSubsetOf:

let itemsSet = Set(Items)
let favoritesSet = Set(favoriteItems)
let result = favoritesSet.isSubsetOf(itemsSet)

Solution 2:[2]

It's very simple, use the following method of NSArray

id commonObject = [array1 firstObjectCommonWithArray:array2];

Ref: https://developer.apple.com/documentation/foundation/nsarray/1408825-firstobjectcommonwitharray?language=objc

Solution 3:[3]

NSMutableSet *intersection = [NSMutableSet setWithArray:aArray];
[intersection intersectSet:[NSSet setWithArray:bArray]];
NSArray *intrsecArray = [intersection allObjects];

Subset of objects in bArray that are not present in aArray:

NSMutableArray *cArray = [NSMutableArray arrayWithArray:bArray];
[cArray removeObjectsInArray:aArray];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pieter Kuijpers
Solution 2 Shaik Riyaz
Solution 3 halfer