Swift Comparison in consecutive numbers inside array and its count [duplicate]
This question already has an answer here:
Swift: What's the best way to pair up elements of an Array
3 answers
How can I compare the two consecutive numbers in an array and find its count.
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]
For example:
First, I want to calculate difference of the first two numbers[1,2(difference=1)]in the array, then the next two numbers[4,6(difference=2)], then[7,10(difference=3)] and [12,13(difference=1)] at last.
Lastly, I want to count a number of difference that has 1. In this case the count is 2.
What method should I use for this one?
Thanks in advance.
arrays swift filter
marked as duplicate by Cristik, ozgur, Owen Pauling, DanielBarbarian, Larme Nov 26 '18 at 11:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Swift: What's the best way to pair up elements of an Array
3 answers
How can I compare the two consecutive numbers in an array and find its count.
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]
For example:
First, I want to calculate difference of the first two numbers[1,2(difference=1)]in the array, then the next two numbers[4,6(difference=2)], then[7,10(difference=3)] and [12,13(difference=1)] at last.
Lastly, I want to count a number of difference that has 1. In this case the count is 2.
What method should I use for this one?
Thanks in advance.
arrays swift filter
marked as duplicate by Cristik, ozgur, Owen Pauling, DanielBarbarian, Larme Nov 26 '18 at 11:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Swift: What's the best way to pair up elements of an Array
3 answers
How can I compare the two consecutive numbers in an array and find its count.
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]
For example:
First, I want to calculate difference of the first two numbers[1,2(difference=1)]in the array, then the next two numbers[4,6(difference=2)], then[7,10(difference=3)] and [12,13(difference=1)] at last.
Lastly, I want to count a number of difference that has 1. In this case the count is 2.
What method should I use for this one?
Thanks in advance.
arrays swift filter
This question already has an answer here:
Swift: What's the best way to pair up elements of an Array
3 answers
How can I compare the two consecutive numbers in an array and find its count.
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]
For example:
First, I want to calculate difference of the first two numbers[1,2(difference=1)]in the array, then the next two numbers[4,6(difference=2)], then[7,10(difference=3)] and [12,13(difference=1)] at last.
Lastly, I want to count a number of difference that has 1. In this case the count is 2.
What method should I use for this one?
Thanks in advance.
This question already has an answer here:
Swift: What's the best way to pair up elements of an Array
3 answers
arrays swift filter
arrays swift filter
edited Nov 26 '18 at 12:08
aka ak
asked Nov 26 '18 at 8:19
aka ak aka ak
7729
7729
marked as duplicate by Cristik, ozgur, Owen Pauling, DanielBarbarian, Larme Nov 26 '18 at 11:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Cristik, ozgur, Owen Pauling, DanielBarbarian, Larme Nov 26 '18 at 11:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
From this answer by Martin R, you can check how to create pairs as below,
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])}
Now you can create differences array and find the one's count as below,
let differences = output.map({ $0.1 - $0.0 })
let onesCount = differences.filter({ $0 == 1}).count
print(differences)
print(onesCount)
Output
[1, 2, 3, 1]
2
1
hi, thanks. this worked like a charm! this one is very simple and easy to follow
– aka ak
Nov 26 '18 at 12:13
add a comment |
I'm sure there are nicer ways to do this (but it's Monday morning).
One easy solution is to loop through the array using a stride, allowing you to take steps off two.
You then append each difference to a new difference array.
And finally use a filter on this resulting array to determine how often that difference occurs.
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)
add a comment |
Good answer by @Philip. Here is an updated solution also handled other cases.
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)
add a comment |
You can achieve this with a 2 lines of code using zip
compactMap
and reduce
:
First we create a tuple of consecutive elements, we use zip
in order to use the index of the element and compactMap
to filter nil
elements then we reduce the new array to count only the tuples with a difference of 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})
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
From this answer by Martin R, you can check how to create pairs as below,
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])}
Now you can create differences array and find the one's count as below,
let differences = output.map({ $0.1 - $0.0 })
let onesCount = differences.filter({ $0 == 1}).count
print(differences)
print(onesCount)
Output
[1, 2, 3, 1]
2
1
hi, thanks. this worked like a charm! this one is very simple and easy to follow
– aka ak
Nov 26 '18 at 12:13
add a comment |
From this answer by Martin R, you can check how to create pairs as below,
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])}
Now you can create differences array and find the one's count as below,
let differences = output.map({ $0.1 - $0.0 })
let onesCount = differences.filter({ $0 == 1}).count
print(differences)
print(onesCount)
Output
[1, 2, 3, 1]
2
1
hi, thanks. this worked like a charm! this one is very simple and easy to follow
– aka ak
Nov 26 '18 at 12:13
add a comment |
From this answer by Martin R, you can check how to create pairs as below,
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])}
Now you can create differences array and find the one's count as below,
let differences = output.map({ $0.1 - $0.0 })
let onesCount = differences.filter({ $0 == 1}).count
print(differences)
print(onesCount)
Output
[1, 2, 3, 1]
2
From this answer by Martin R, you can check how to create pairs as below,
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])}
Now you can create differences array and find the one's count as below,
let differences = output.map({ $0.1 - $0.0 })
let onesCount = differences.filter({ $0 == 1}).count
print(differences)
print(onesCount)
Output
[1, 2, 3, 1]
2
edited Nov 26 '18 at 12:19
answered Nov 26 '18 at 8:54
KamranKamran
6,71521028
6,71521028
1
hi, thanks. this worked like a charm! this one is very simple and easy to follow
– aka ak
Nov 26 '18 at 12:13
add a comment |
1
hi, thanks. this worked like a charm! this one is very simple and easy to follow
– aka ak
Nov 26 '18 at 12:13
1
1
hi, thanks. this worked like a charm! this one is very simple and easy to follow
– aka ak
Nov 26 '18 at 12:13
hi, thanks. this worked like a charm! this one is very simple and easy to follow
– aka ak
Nov 26 '18 at 12:13
add a comment |
I'm sure there are nicer ways to do this (but it's Monday morning).
One easy solution is to loop through the array using a stride, allowing you to take steps off two.
You then append each difference to a new difference array.
And finally use a filter on this resulting array to determine how often that difference occurs.
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)
add a comment |
I'm sure there are nicer ways to do this (but it's Monday morning).
One easy solution is to loop through the array using a stride, allowing you to take steps off two.
You then append each difference to a new difference array.
And finally use a filter on this resulting array to determine how often that difference occurs.
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)
add a comment |
I'm sure there are nicer ways to do this (but it's Monday morning).
One easy solution is to loop through the array using a stride, allowing you to take steps off two.
You then append each difference to a new difference array.
And finally use a filter on this resulting array to determine how often that difference occurs.
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)
I'm sure there are nicer ways to do this (but it's Monday morning).
One easy solution is to loop through the array using a stride, allowing you to take steps off two.
You then append each difference to a new difference array.
And finally use a filter on this resulting array to determine how often that difference occurs.
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)
answered Nov 26 '18 at 8:38
Philip De VriesPhilip De Vries
25539
25539
add a comment |
add a comment |
Good answer by @Philip. Here is an updated solution also handled other cases.
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)
add a comment |
Good answer by @Philip. Here is an updated solution also handled other cases.
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)
add a comment |
Good answer by @Philip. Here is an updated solution also handled other cases.
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)
Good answer by @Philip. Here is an updated solution also handled other cases.
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)
answered Nov 26 '18 at 9:11
iDev750iDev750
5931315
5931315
add a comment |
add a comment |
You can achieve this with a 2 lines of code using zip
compactMap
and reduce
:
First we create a tuple of consecutive elements, we use zip
in order to use the index of the element and compactMap
to filter nil
elements then we reduce the new array to count only the tuples with a difference of 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})
add a comment |
You can achieve this with a 2 lines of code using zip
compactMap
and reduce
:
First we create a tuple of consecutive elements, we use zip
in order to use the index of the element and compactMap
to filter nil
elements then we reduce the new array to count only the tuples with a difference of 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})
add a comment |
You can achieve this with a 2 lines of code using zip
compactMap
and reduce
:
First we create a tuple of consecutive elements, we use zip
in order to use the index of the element and compactMap
to filter nil
elements then we reduce the new array to count only the tuples with a difference of 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})
You can achieve this with a 2 lines of code using zip
compactMap
and reduce
:
First we create a tuple of consecutive elements, we use zip
in order to use the index of the element and compactMap
to filter nil
elements then we reduce the new array to count only the tuples with a difference of 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})
answered Nov 26 '18 at 10:29
inspector_60inspector_60
4281312
4281312
add a comment |
add a comment |