## Swift Comparison in consecutive numbers inside array and its count

**This question already has answers here**:

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

**Check if array elements are consecutive,** 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 Following is a quick example to get the count of elements present in the array. array_name is the array variable name. count returns an integer value for the size of this array. Example to get the size of an Array in Swift using count function In the following example, we have two arrays. First array values is empty. Second array numbers is an integer array with three elements.

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)

**iOS 13 Programming Fundamentals with Swift: Swift, Xcode, and ,** 2) Do a linear scan of the sorted array. If the difference between current element and next element is anything other than 1, then return false. If all differences are 1, What if we wanted our program to work with any number of grades between 1 grade and 10 grades. It’s not practical to write a separate program for each case. We would like to have something like a list of grades. This list would contain any number of grades. This is where arrays come in. What is an array?

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)

**Sum of consecutive two elements in a array,** Swift, Xcode, and Cocoa Basics Matt Neuburg this chapter), nested arrays can be compared with == as long as the inner array's elements are Equatable. Basic array properties and methods An array is a Collection, which is itself a Sequence. array's count read-only property reports the number of elements it contains. Basic Operators¶. An operator is a special symbol or phrase that you use to check, change, or combine values. For example, the addition operator (+) adds two numbers, as in let i = 1 + 2, and the logical AND operator (&&) combines two Boolean values, as in if enteredDoorCode && passedRetinaScan.

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})

**Array,** Given an array print sum of the pairwise consecutive elements. traverse the array and saving the sum of consecutive numbers in the variable sum. if elements of an array can be arranged in a Circle with consecutive difference as 1 integer into a subarray · Replace every element with the smallest of the others · Count - Get array element at index - Get first element of array - Get last element of array - Get maximum element from array - Get minimum element from array - Get the size of an array - Insert item at array index - Join array elements using separator - Loop through array - Loop through array with index - Map an array to another array - Pop last item

**ContiguousArray,** Specifically, you use the Array type to hold elements of a single type, the array's Using a negative number or an index equal to or greater than count triggers a Inserts the elements of a sequence into the collection at the specified position. the sequence, using the given predicate as the comparison between elements. You are given an array of n+2 elements. All elements of the array are in range 1 to n. And all elements occur once except two numbers which occur twice. Find the two repeating numbers. The above array has n + 2 = 7 elements with all elements occurring once except 2 and 4 which occur twice. So the output should be 4 2.

**Implementing binary search of an array (article),** Creates an array containing the elements of a sequence. var count: Int. The func difference<C>(from: C, by: (C.Element, Element) -> Bool) Encodes the elements of this contiguous array into the given encoder in an unkeyed container. If you want to count how many items in an array (or any collection) match a test you specify, the easiest thing to do is run the collection through a call to filter() then count the remainder. For example, if you had an array of numbers and wanted to count how many were odd, you would write this:

**Array,** Array indices start at 0 and count upwards. Because the array primes contains 25 numbers, the indices into the array range could, but the indices guessed by binary search don't go in the sequential order that a iOS app · Android app Maximum consecutive one’s (or zeros) in a binary circular array; Maximum number of consecutive 1's in binary representation of all the array elements; Maximum difference of zeros and ones in binary string; Maximum difference of zeros and ones in binary string | Set 2 (O(n) time) Binary Array Range Queries to find the minimum distance between two Zeros

##### Comments

- hi, thanks. this worked like a charm! this one is very simple and easy to follow