Created
September 3, 2018 22:32
-
-
Save thexande/c4336b75031da6576134122d7b8cc77f to your computer and use it in GitHub Desktop.
Array Intersection: Find the intersection of a given set of 3 sorted arrays
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
/// Array Intersection: Find the intersection of a given set of 3 sorted arrays | |
/// | |
/// - Parameters: | |
/// - arrayOne: the first sorted array of Integers | |
/// - arrayTwo: the second sorted array of Integers | |
/// - arrayThree: the third sorted array of Integers | |
/// - Returns: an array of intersection points within the given 3 arrays | |
func findIntersection(arrayOne: [Int], arrayTwo: [Int], arrayThree: [Int]) -> [Int] { | |
var results: [Int] = [] | |
var x = 0 | |
var y = 0 | |
var z = 0 | |
var oob: Bool { | |
return (x >= arrayOne.count || y >= arrayTwo.count || z >= arrayThree.count) | |
} | |
while !oob { | |
if arrayOne[x] == arrayTwo[y] && arrayTwo[y] == arrayThree[z] { | |
results.append(arrayOne[x]) | |
x += 1 | |
y += 1 | |
z += 1 | |
} else if arrayOne[x] < arrayTwo[y] { | |
x += 1 | |
} else if arrayTwo[y] < arrayThree[z] { | |
y += 1 | |
} else { | |
z += 1 | |
} | |
} | |
return results | |
} | |
let arrayOne = [2, 6, 9, 11, 13, 49] | |
let arrayTwo = [3, 6, 7, 10, 13, 18] | |
let arrayThree = [4, 5, 6, 9, 11, 13] | |
let intersect = findIntersection(arrayOne: arrayOne, arrayTwo: arrayTwo, arrayThree: arrayThree) | |
print(intersect) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment