Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 234 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to reverse array in Swift without using ".reverse()"?

#1
I have array and need to reverse it without `Array.reverse` method, only with a `for` loop.

var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
Reply

#2
**Swift 5:**
```
let names: [String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]

var reversenames: [String] = []

let count = names.count

for index in 0..<count {
reversenames.insert(names[count-index-1], at: index)
}

print(reversenames)
```
Reply

#3
The most efficient way is to `swap` the items at `start`- and `endIndex` and move the indices bidirectional to the middle. This is a generic version

extension Array {
mutating func upsideDown() {
if isEmpty { return }
var 👉 = startIndex
var 👈 = index(before: endIndex)
while 👉 < 👈 {
swapAt(👉, 👈)
formIndex(after: &👉)
formIndex(before: &👈)
}
}
}
Reply

#4
var names = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]

// 'while' loop
var index = 0
let totalIndices = names.count - 1
while index < names.count / 2 {
names.swapAt(index, totalIndices-index)
index += 1
}

// 'for' loop
for index in 0..<names.count/2 {
names.swapAt(index, names.count-index-1)
}

// output: "["Asus", "Lenovo", "Sony", "Microsoft", "Apple"]"
Reply

#5
First, need to find the middle of array. This method is faster than the linear time O(n) and slower than the constant time O(1) complexity.

func reverse<T>(items: [T]) -> [T] {
var reversed = items
let count = items.count

let middle = count / 2

for i in stride(from: 0, to: middle, by: 1) {
let first = items[i]
let last = items[count - 1 - i]
reversed[i] = last
reversed[count - 1 - i] = first
}

return reversed
}
Reply

#6
Recently I had an interview and I was asked this question, how to reverse an array without using **reversed()**. Here is my solution below:

func reverseArray( givenArray:inout [Int]) -> [Int] {
var reversedArray = [Int]()
while givenArray.count > 0 {
reversedArray.append(givenArray.removeLast())
}
return reversedArray
}

var array = [1,2,3,4,5,6,7]
var reversed = reverseArray(givenArray: &array)
Reply

#7
var names:[String] = [ "A", "B", "C", "D", "E","F","G"]
var c = names.count - 1
var i = 0
while i < c {
swap(&names[i++],&names[c--])
}

**Cristik**

while i < c {
swap(&names[i],&names[c]
i += 1
c -= 1

}
Reply

#8
Edited as generic

// Swap the first index with the last index.
// [1, 2, 3, 4, 5] -> pointer on one = array[0] and two = array.count - 1
// After first swap+loop increase the pointer one and decrease pointer two until
// conditional is not true.

func reverse<T>(with array: [T]) -> [T] {
var array = array
var first = 0
var last = array.count - 1
while first < last {
array.swapAt(first, last)
first += 1
last -= 1
}
return array
}

input-> [1, 2, 3, 4, 5] output ->[5, 4, 3, 2, 1]
input-> ["a", "b", "c", "d"] output->["d", "c", "b", "a"]

// Or a shorter version divide and conquer

func reversed<T>(with arr: [T]) -> [T] {
var arr = arr
(0..<arr.count / 2).forEach { i in
arr.swapAt(i, arr.count - i - 1)
}
return arr
}
Reply

#9
Ignoring checks for emptiness..


var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]

var reversedNames = [String]()

for name in names {
reversedNames.insert((name), at:0)
}

print(reversedNames)
Reply

#10
```
var arr = [1, 2, 3, 4, 5] // Array we want to reverse
var reverse: [Int]! // Array where we store reversed values

reverse = arr

for i in 0...(arr.count - 1) {

reverse[i] = arr.last! // Adding last value of original array at reverse[0]
arr.removeLast() // removing last value & repeating above step.
}

print("Reverse : \(reverse!)")
```
**A more simple way :)**
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through