Skip to content

Instantly share code, notes, and snippets.

@farhanf
Created April 11, 2019 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farhanf/01b6ef38662ec0e721c43d648b8d2480 to your computer and use it in GitHub Desktop.
Save farhanf/01b6ef38662ec0e721c43d648b8d2480 to your computer and use it in GitHub Desktop.
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