Search This Blog

Sunday, October 8, 2017

iOS. How to shift array in swift


Let's assume the problem: we have an array of elements, no matter what, numbers or rows or any other. It is necessary to move the array several positions to the left. For example:

source array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

shifted array [3, 4, 5, 6, 7, 8, 9, 10, 1, 2]

We shift array left to 2 elements, so now third element with index 2 is become first with index 0.


Shifting with using for loop 
import UIKit
import Foundation

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
//needed result = [3, 4, 5, 6, 7, 8, 9, 10, 1, 2]

let neededNumber = 3
var firstPart = [Int]()
var secondPart = [Int]()
for number in numbers {
    if number == neededNumber || firstPart.count > 0 {
        firstPart.append(number)
    } else {
        secondPart.append(number)
    }
}

let result = firstPart + secondPart
// result = [3, 4, 5, 6, 7, 8, 9, 10, 1, 2]


Shifting with index, prefix and suffix methods
let numbersArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
//needed result = [3, 4, 5, 6, 7, 8, 9, 10, 1, 2]

let index = numbersArray.index(where: { $0 == 3 })
let prefix = numbersArray.prefix(upTo: index!)
// not include element at the end position
// prefix = [1, 2]

let suffix = numbersArray.suffix(from: index!)
// from specified postion to the end of array
// suffix = [3, 4, 5, 6, 7, 8, 9, 10]

let shiftingArray = suffix  + prefix
// shiftingArray = [3, 4, 5, 6, 7, 8, 9, 10, 1, 2]

GitHub Link

8 comments:

  1. Your article is one of its kind which explained every bit of Custom Build Website. looking for further valuable articles from you

    ReplyDelete
  2. Thank you very much for drawing my notice to this. Your blog is jam-packed with helpful information. Unless I read it, I'd have no idea. I'll be back for more great content. Wishing you the best of luck and prosperity. Best Custom Websites

    ReplyDelete
  3. Great content. I was looking for this kind of content. It saved my time to search further.
    You must provide such type of content constantly. I appreciate your willingness to provide readers with a good article.
    Custom Design Websites

    ReplyDelete
  4. Thank you very much for sharing this informational blog.I could not find such kind of information in any other site.I'll come back for more fantastic material.
    Best wishes, and good luck.
    Custom Website

    ReplyDelete
  5. your article is unbelievable with accurate information. I see a lot of research after that and that is more than I expectedCustom Build Website

    ReplyDelete
  6. Thank you very much for drawing my notice to this. Your blog is jam-packed with helpful information. Unless I read it, I'd have no idea. I'll be back for more great content. Wishing you the best of luck and prosperity.
    Mobile Performance Meter HackYou made some good points there. I did a Google search about the topic and found most people will believe your blog

    ReplyDelete
  7. I adore your work, and it greatly motivates me.
    SEO Services NYC

    ReplyDelete