Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 数组中连续数字的快速比较及其计数_Arrays_Swift_Filter - Fatal编程技术网

Arrays 数组中连续数字的快速比较及其计数

Arrays 数组中连续数字的快速比较及其计数,arrays,swift,filter,Arrays,Swift,Filter,如何比较数组中的两个连续数字并找到其计数 let numberArray = [1,2,4,6,7,10,12,13] // I want to compare two consecutive numbers like [1,2], [4,6], [7,10], [12,13] 例如: 首先,我想计算数组中前两个数字[1,2(差=1)]的差,然后是下两个数字[4,6(差=2)],最后是[7,10(差=3)]和[12,13(差=1)]。 最后,我想计算一个有1的差。在本例中,计数为2 我应该用什

如何比较数组中的两个连续数字并找到其计数

let numberArray = [1,2,4,6,7,10,12,13]
// I want to compare two consecutive numbers like [1,2], [4,6], [7,10], [12,13]
例如:
首先,我想计算数组中前两个数字[1,2(差=1)]的差,然后是下两个数字[4,6(差=2)],最后是[7,10(差=3)]和[12,13(差=1)]。
最后,我想计算一个有1的差。在本例中,计数为2

我应该用什么方法来做这个


提前谢谢

我相信有更好的方法可以做到这一点(但现在是星期一早上)。 一个简单的解决方案是使用跨步在阵列中循环,允许您从两步中跳出来。 然后将每个差异附加到新的差异数组。 最后,在这个结果数组上使用一个过滤器来确定这种差异发生的频率

let difference      = 1
let array           = [1,2,4,6,7,10,12,13]
var differenceArray = [Int]()
for index in stride(from: 1, to: array.count, by: 2) {
    let difference  = array[index]-array[index-1]
    differenceArray.append(difference)
}

print(differenceArray.filter{ $0 == difference }.count)
从这里,您可以检查如何创建对,如下所示

let input = [1,2,4,6,7,10,12,13]
let output = stride(from: 0, to: input.count - 1, by: 2).map{(input[$0], input[$0 + 1])}
let differences = output.map({ $0.1 - $0.0 })
let onesCount = differences.filter({ $0 == 1}).count

print(differences)
print(onesCount)
现在,您可以创建差异数组并找到一的计数,如下所示

let input = [1,2,4,6,7,10,12,13]
let output = stride(from: 0, to: input.count - 1, by: 2).map{(input[$0], input[$0 + 1])}
let differences = output.map({ $0.1 - $0.0 })
let onesCount = differences.filter({ $0 == 1}).count

print(differences)
print(onesCount)
输出

[1, 2, 3, 1]
2

菲利普的回答很好。这是一个更新的解决方案,也处理了其他情况

let numbers = [1, 2, 5, 4, 10, 6, 7, 8, 11, 10, 23]
var allDifference: [Int] = []
for index in stride(from: 0, to: numbers.count, by: 2) {
   let firstValue = numbers[index]
   let secondValue = ((index == numbers.count - 1 && numbers.count % 2 != 0) ? 0 : numbers[index + 1])
   allDifference.append(abs(firstValue - secondValue))
}

let oneDifferenceCount = allDifference.filter { $0 == 1 }.count
print("Result: ", oneDifferenceCount)

您可以使用
zip
compactMap
reduce
使用两行代码来实现这一点:

首先,我们创建一个连续元素的元组,我们使用
zip
来使用元素的索引和
compactMap
来过滤
nil
元素,然后我们减少新数组,只计算差值为1的元组

//Create tuples of consecutive values
let tuples = zip(numberArray.indices, numberArray).compactMap{$0 % 2 == 0 ? nil : (numberArray[$0-1],$1) }
// reduce to count only the tuples with difference of 1
let diffOneCount = tuples.reduce(0,{$1.0+1 == $1.1 ? $0+1 : $0})

嗨,谢谢。这很有魅力!这个很简单,也很容易理解