我爲您創建了一個simle示例,顯示如何找到丟失的元素的位置。
let first = [1,2,6,8,9,10]
let second = [1,2,3,4,5,6,7,8,9,10]
func fetchIndexesWithoutConsecutive(firstArray: [Int], secondArray: [Int]) -> [Int] {
//Output array
var array = [Int]()
//Enumerate second array and compare if first array conteins this elments if not we add postion of the missing item
for (index, value) in secondArray.enumerate() {
if (!firstArray.contains(value)) {
array.append(index)
}
}
guard array.count > 1 else {
return array
}
var result = [Int]()
for i in 0...array.count-2 {
let first = array[i]
let second = array[i+1]
if second - first > 1 {
result.append(second)
}
}
return result // [6] position of the elements in second array that are not in first array without consecutive
}
fetchIndexesWithoutConsecutive(first, secondArray: second)
謝謝,但它並沒有完全解決我的問題。您的代碼將提供所有免費空間,但我只對那些擁有單個空閒空間的人感興趣。用我的'allPos'和'selectedPos'的例子,你提供的代碼將產生'[point2,point3,point5,point8]'。我只需要單個空閒空間(沒有連續的空閒空間),它們是'[point5,point8]''。我希望這可以更好地解釋事情 – Hilarious404
難以理解你能否用真實數據描述點 –
假設我有兩個'Int'數組'allPos = [1,2,3,4,5,6,7,8,9, 10]'和'selectedPos = [1,4,6,7,9,10]'按給定順序排列(升序)。當比較'allPos'和'selectedPos'時,未選擇的元素是'[2,3,5,8]'。 '2','3'是連續的元素,我不想在結果中使用連續的元素。但'5'和'8'不是連續的。所以我對'[5,8]'感興趣,因爲它們是唯一未被選擇的不連續的項目。 – Hilarious404