Created
April 11, 2019 02:53
-
-
Save farhanf/01b6ef38662ec0e721c43d648b8d2480 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isSubsetOf(arr1: [String], arr2: [String]) -> Bool { | |
var result = false | |
arr2.forEach { (str) in | |
conditional: if (arr1.contains(str)) { | |
result = true | |
} else { | |
result = false | |
break conditional | |
} | |
} | |
return result | |
} | |
// Faster way using built in set function which relies on Hashtables | |
func isSubsetOfFast(arr1: [String], arr2: [String]) -> Bool{ | |
var result = false | |
let set1 = Set(arr1) | |
let set2 = Set(arr2) | |
if (set2.isSubset(of: set1)){ | |
result = true | |
} | |
return result | |
} | |
let testArr1 = ["A","B","C","D","E"] | |
let testArr2 = ["A","E","D"] | |
let testArr3 = ["A","B","C","D","E"] | |
let testArr4 = ["A","D","Z"] | |
print(isSubsetOf(arr1: testArr3, arr2: testArr4)) | |
print(isSubsetOfFast(arr1: testArr3, arr2: testArr4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment