Swift Comparison in consecutive numbers inside array and its count [duplicate]












1
















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.










share|improve this 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.























    1
















    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.










    share|improve this 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.





















      1












      1








      1









      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.










      share|improve this question

















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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.


























          4 Answers
          4






          active

          oldest

          votes


















          0














          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





          share|improve this answer





















          • 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



















          0














          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)





          share|improve this answer































            0














            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)





            share|improve this answer































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





              share|improve this answer






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                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





                share|improve this answer





















                • 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
















                0














                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





                share|improve this answer





















                • 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














                0












                0








                0







                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





                share|improve this answer















                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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                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














                • 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













                0














                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)





                share|improve this answer




























                  0














                  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)





                  share|improve this answer


























                    0












                    0








                    0







                    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)





                    share|improve this answer













                    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)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 26 '18 at 8:38









                    Philip De VriesPhilip De Vries

                    25539




                    25539























                        0














                        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)





                        share|improve this answer




























                          0














                          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)





                          share|improve this answer


























                            0












                            0








                            0







                            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)





                            share|improve this answer













                            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)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 26 '18 at 9:11









                            iDev750iDev750

                            5931315




                            5931315























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





                                share|improve this answer




























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





                                  share|improve this answer


























                                    0












                                    0








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





                                    share|improve this answer













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






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 26 '18 at 10:29









                                    inspector_60inspector_60

                                    4281312




                                    4281312















                                        Popular posts from this blog

                                        Contact image not getting when fetch all contact list from iPhone by CNContact

                                        count number of partitions of a set with n elements into k subsets

                                        A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks