Swift inputing characters and strings into a dictionary - arrays

How would I go about Writing a function that takes in an array of strings and outputs a dictionary with the first character of each string as a key and the remaining as objects?

Writing this piece of code for you without seeing any trials from your side, just because you are new on StackOverflow. And I'm seeing the message from StackOverflow that "Corey Townsend is a new contributor. Be nice..."
So I'm just being nice for you as a welcome and here is your code.
let arr = ["car", "cat", "dog", "ball", "flower", "notebook", "fire"]
func createDictionaryFromArrayAsCoreyWants(arr:[String]) -> [String:String] {
var dict:[String:String] = [:]
arr.forEach({ (word:String) in
let strKey = String(word.prefix(1))
let startIndex: String.Index = word.index(word.startIndex, offsetBy: 1)
let strValue = String(word[startIndex..<word.endIndex])
dict[strKey] = strValue
print(strKey + " : " + strValue)
})
return dict
}
let d = createDictionaryFromArrayAsCoreyWants(arr: arr)
print(d)
Addition
Just saw a comment on another answer by 'Alex Bailer' so adding one more function for you. Enjoy...
func createDictionaryFromArrayAsCoreyWants(arr:[String]) -> [String:[String]] {
var dict:[String:[String]] = [:]
arr.forEach({ (word:String) in
let strKey = String(word.prefix(1))
let startIndex: String.Index = word.index(word.startIndex, offsetBy: 1)
let strValue = String(word[startIndex..<word.endIndex])
if let a = dict[strKey] {
dict[strKey] = a + [strValue]
} else {
dict[strKey] = [strValue]
}
print(strKey + " : " + strValue)
})
return dict
}
let d = createDictionaryFromArrayAsCoreyWants(arr: arr)
print(d)
Output:
["d": ["og"], "n": ["otebook"], "b": ["all"], "c": ["ar", "at"], "f": ["lower", "ire"]]

Is this what are you looking for?
let d = Dictionary(grouping: array, by: {$0.prefix(1)})
with array:
let array = ["car", "cat", "dog", "ball", "flower", "notebok", "fire"]
It prints:
["b": ["ball"], "f": ["flower", "fire"], "d": ["dog"], "c": ["car", "cat"], "n": ["notebok"]]
And then, remove the first letter from values:
for key in d.keys {
let values = d[key]
let start = String.Index(encodedOffset: 1)
d[key] = values?.compactMap({ String($0[start...]) })
}
Output:
["f": ["lower", "ire"], "d": ["og"], "b": ["all"], "c": ["ar", "at"], "n": ["otebok"]]

Related

how to combine two dictionary values as a key and value pair in swift

let arra = ["abc","def","abc","def"]
let arra2 = ["addr1","addr2","addr1","addr2"]
Expected Output
dic = ["abc":"addr1","addr1" , def: "addr2","addr2"]
Swift 4's new Dictionary initializer lets you do that kind of thing easily:
let arra = ["abc","def","abc","def"]
let arra2 = ["addr1","addr2","addr1","addr2"]
let dict = [String:[String]](zip(arra,arra2.map{[$0]}),uniquingKeysWith:+)
print(dict) // ["abc": ["addr1", "addr1"], "def": ["addr2", "addr2"]]
[EDIT] Swift 3 equivalent :
var dict : [String:[String]] = [:]
zip(arra,arra2.map{[$0]}).forEach{ dict[$0] = (dict[$0] ?? []) + $1 }
There should be easier way but in general:
import UIKit
let keyArray = ["abc","def","abc","def"]
let valueArray = ["addr1","addr2","addr1","addr2"]
let setFromKeyArray = Set(keyArray)
var finalDict = [String: [String]]()
for index in 0..<keyArray.count {
if let _ = finalDict[keyArray[index]] {
finalDict[keyArray[index]]!.append(valueArray[index])
} else {
finalDict[keyArray[index]] = [valueArray[index]]
}
}
print(finalDict)
// output: ["abc": ["addr1", "addr1"], "def": ["addr2", "addr2"]]
Using zip(_:_:) and reduce(_:_:):
let array1 = ["abc", "def", "abc", "def"]
let array2 = ["addr1", "addr2", "addr1", "addr2"]
let dictionary = zip(array1, array2).reduce([String: String]()) {
var dictionary = $0
dictionary[$1.0] = $1.1
return dictionary
}
print(dictionary) // ["abc": "addr1", "def": "addr2"]
You can use like below :
let arra = ["abc","def","abc","def"]
let arra2 = ["addr1","addr2","addr1","addr2"]
var dictionary: [String: String] = [:]
dictionary.merge(zip(arra, arra2)) { (old, new) -> String in
return "\(old), \(new)"
}
print(dictionary)
Output :
["abc": "addr1, addr1", "def": "addr2, addr2"]
The best of Dennis and Kristijan and Alain.
let arra = ["abc", "def", "abc", "def"]
let arra2 = ["addr1", "addr2", "addr1", "addr2"]
let dict = zip(arra, arra2).reduce([String:[String]]()){
var d = $0
d[$1.0] = ($0[$1.0] ?? []) + [$1.1]
return d
}
print(dict) // ["def": ["addr2", "addr2"], "abc": ["addr1", "addr1"]]
Remember dictionary is unordered.

Convert String containing Array of Integer to Integer Array IN Swift

I have a
let locationjson: String = "[\"43786\",\"55665\",\"62789\",\"90265\"]"
And I want to convert it to Arraylist / List in Swift... I have searched on StackOverflow but couldn't find an appropriate solution for Swift.
I want to output as List<Integer> containing the values [43786,55665,62789,90265].
As Martin mentioned in the comments JSONSerialization is your friend:
let locationjson = "[\"43786\",\"55665\",\"62789\",\"90265\"]"
let data = locationjson.data(using: .utf8)!
if let array = (try? JSONSerialization.jsonObject(with: data)) as? [String] {
let intArray = array.flatMap { Int($0) }
print(intArray)
}
You can do this using flatMap:
let locationjson = ["43786", "55665", "62789", "90265"]
let result = locationjson.flatMap { Int($0) }
You mean something like this?
var str:String = "[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\"]"
str = str.replacingOccurrences(of: "[", with: "")
str = str.replacingOccurrences(of: "]", with: "")
str = str.replacingOccurrences(of: "\"", with: "")
var arrStrNums = str.components(separatedBy: ",")
var nums:[Int] = []
for strNum in arrStrNums {
if let num = Int(strNum) {
nums.append(num)
}
}
print("Number list: \(nums)")
Outputs:
Number list: [1, 2, 3, 4, 5, 6]

Swift 3: Array to Dictionary?

I have a large array and need to access it by a key (a lookup) so I need to create Dictionary. Is there a built in function in Swift 3.0 to do so, or do I need to write it myself?
First I will need it for a class with key "String" and later on maybe I will be able to write a template version for general purpose (all types of data and key).
Note for 2019. This is now simply built-in to Swift 5, uniqueKeysWithValues and similar calls.
Is that it (in Swift 4)?
let dict = Dictionary(uniqueKeysWithValues: array.map{ ($0.key, $0) })
Note:
As mentioned in the comment, using uniqueKeysWithValues would give a fatal error (Fatal error: Duplicate values for key: 'your_key':) if you have duplicated keys.
If you fear that may be your case, then you can use init(_:uniquingKeysWith:) e.g.
let pairsWithDuplicateKeys = [("a", 1), ("b", 2), ("a", 3), ("b", 4)] // or `let pairsWithDuplicateKeys = array.map{ ($0.key, $0) }`
let firstValues = Dictionary(pairsWithDuplicateKeys, uniquingKeysWith: { (first, _) in first })
print(firstValues)
//prints ["a": 1, "b": 2]
let lastValues = Dictionary(pairsWithDuplicateKeys, uniquingKeysWith: { (_, last) in last })
print(lastValues)
//prints ["a": 3, "b": 4]
On Swift 4, you can achieve this by using Dictionary's grouping:by: initializer
For ex:
You have class named A
class A {
var name: String
init(name: String) {
self.name = name
}
// .
// .
// .
// other declations and implementions
}
Next, you have an array of objects of type A
let a1 = A(name: "Joy")
let a2 = A(name: "Ben")
let a3 = A(name: "Boy")
let a4 = A(name: "Toy")
let a5 = A(name: "Tim")
let array = [a1, a2, a3, a4, a5]
Let's say you want to create a Dictionary by grouping all the names by their first letter. You use Swifts Dictionary(grouping:by:) to achieve this
let dictionary = Dictionary(grouping: array, by: { $0.name.first! })
// this will give you a dictionary
// ["J": [a1], "B": [a2, a3], "T": [a4, a5]]
source
Note however that the resulting Dictionary "dictionary" is of type
[String : [A]]
it is not of type
[String : A]
as you may expect. (Use #uniqueKeysWithValues to achieve the latter.)
I think you're looking for something like this:
extension Array {
public func toDictionary<Key: Hashable>(with selectKey: (Element) -> Key) -> [Key:Element] {
var dict = [Key:Element]()
for element in self {
dict[selectKey(element)] = element
}
return dict
}
}
You can now do:
struct Person {
var name: String
var surname: String
var identifier: String
}
let arr = [Person(name: "John", surname: "Doe", identifier: "JOD"),
Person(name: "Jane", surname: "Doe", identifier: "JAD")]
let dict = arr.toDictionary { $0.identifier }
print(dict) // Result: ["JAD": Person(name: "Jane", surname: "Doe", identifier: "JAD"), "JOD": Person(name: "John", surname: "Doe", identifier: "JOD")]
If you'd like your code to be more general, you could even add this extension on Sequence instead of Array:
extension Sequence {
public func toDictionary<Key: Hashable>(with selectKey: (Iterator.Element) -> Key) -> [Key:Iterator.Element] {
var dict: [Key:Iterator.Element] = [:]
for element in self {
dict[selectKey(element)] = element
}
return dict
}
}
Do note, that this causes the Sequence to be iterated over and could have side effects in some cases.
As others already said, we need to understand which are the keys.
However I am trying to provide a solution to my interpretation of your question.
struct User {
let id: String
let firstName: String
let lastName: String
}
Here I am assuming that 2 users with the same id cannot exist
let users: [User] = ...
let dict = users.reduce([String:User]()) { (result, user) -> [String:User] in
var result = result
result[user.id] = user
return result
}
Now dict is a dictionary where the key is the user id and the value is the user value.
To access a user via its id you can now simply write
let user = dict["123"]
Update #1: General approach
Given an array of a given type Element, and a closure that determine the key of an Element, the following generic function will generate a Dictionary of type [Key:Element]
func createIndex<Key, Element>(elms:[Element], extractKey:(Element) -> Key) -> [Key:Element] where Key : Hashable {
return elms.reduce([Key:Element]()) { (dict, elm) -> [Key:Element] in
var dict = dict
dict[extractKey(elm)] = elm
return dict
}
}
Example
let users: [User] = [
User(id: "a0", firstName: "a1", lastName: "a2"),
User(id: "b0", firstName: "b1", lastName: "b2"),
User(id: "c0", firstName: "c1", lastName: "c2")
]
let dict = createIndex(elms: users) { $0.id }
// ["b0": {id "b0", firstName "b1", lastName "b2"}, "c0": {id "c0", firstName "c1", lastName "c2"}, "a0": {id "a0", firstName "a1", lastName "a2"}]
Update #2
As noted by Martin R the reduce will create a new dictionary for each iteration of the related closure. This could lead to huge memory consumption.
Here's another version of the createIndex function where the space requirement is O(n) where n is the length of elms.
func createIndex<Key, Element>(elms:[Element], extractKey:(Element) -> Key) -> [Key:Element] where Key : Hashable {
var dict = [Key:Element]()
for elm in elms {
dict[extractKey(elm)] = elm
}
return dict
}
let pills = ["12", "34", "45", "67"]
let kk = Dictionary(uniqueKeysWithValues: pills.map{ ($0, "number") })
["12": "number", "67": "number", "34": "number", "45": "number"]
swift5 swift4
The following converts an array to a dictionary.
let firstArray = [2,3,4,5,5]
let dict = Dictionary(firstArray.map { ($0, 1) } , uniquingKeysWith: +)
Swift 5
extension Array {
func toDictionary() -> [Int: Element] {
self.enumerated().reduce(into: [Int: Element]()) { $0[$1.offset] = $1.element }
}
}
This extension works for all sequences (including arrays) and lets you select both key and value:
extension Sequence {
public func toDictionary<K: Hashable, V>(_ selector: (Iterator.Element) throws -> (K, V)?) rethrows -> [K: V] {
var dict = [K: V]()
for element in self {
if let (key, value) = try selector(element) {
dict[key] = value
}
}
return dict
}
}
Example:
let nameLookup = persons.toDictionary{($0.name, $0)}
Just do it simply,
let items = URLComponents(string: "https://im.qq.com?q=13&id=23")!.queryItems!
var dic = [String: Any?]()
items.foreach {
dic[$0.name] = $0.value
}
reduce is not very suitable,
let dic: [String: Any?] = items.reduce([:]) { (result: [String: Any?], item: URLQueryItem) -> [String: Any?] in
var r = result
r[item.name] = item.value // will create an copy of result!!!!!!
return r
}
As i understand from you're question you would like to convert to Array to Dictionary.
In my case i create extension for the Array and keys for the dictionary will be indexes of the Array.
Example:
var intArray = [2, 3, 5, 3, 2, 1]
extension Array where Element: Any {
var toDictionary: [Int:Element] {
var dictionary: [Int:Element] = [:]
for (index, element) in enumerate() {
dictionary[index] = element
}
return dictionary
}
}
let dic = intArray.toDictionary
Compatible with Swift 5 Standard Library (Xcode 10.2+ , iOS 12.2).
Here's an example of usage of an initializer init(uniqueKeysWithValues:)
The input let array: [String] = Locale.isoRegionCodes is an array of ISO31661-2 codes represented by a string.
let countryCodeAndName: [String: String] = Dictionary(uniqueKeysWithValues: Locale.isoRegionCodes.map { ($0, Locale.current.localizedString(forRegionCode: $0) ?? "")} )
Returned dictionary, will list all regions with ISO31661-2 code as a key and a localized region name as a value.
Output:
...
"PL":"Poland"
"DE":"Germany"
"FR":"France"
"ES":"Spain"
...
Example 2:
let dictionary: [String: String] = Dictionary(uniqueKeysWithValues: [ ("key1", "value1"), ("key2", "value2")] )
Output:
["key1": "value1", "key2": "value2"]
Important:
Precondition: The sequence must not have duplicate keys.
Code below will crash an app:
let digitWords = ["one", "two", "three", "four", "five", "five"]
let wordToValue = Dictionary(uniqueKeysWithValues: zip(digitWords, 1...6))
with:
Fatal error: Duplicate values for key: 'five'
If you want to follow the pattern set out by map and reduce in swift you could do something nice and functional like this:
extension Array {
func keyBy<Key: Hashable>(_ keyFor: (Element) -> Key) -> [Key: Element] {
var ret = [Key: Element]()
for item in self{
ret[keyFor(item)] = item
}
return ret
}
}
Usage:
struct Dog {
let id: Int
}
let dogs = [Dog(id: 1), Dog(id: 2), Dog(id: 3), Dog(id: 4)]
let dogsById = dogs.keyBy({ $0.id })
// [4: Dog(id: 4), 1: Dog(id: 1), 3: Dog(id: 3), 2: Dog(id: 2)]
Swift way:
extension Sequence {
func toDictionary<Key: Hashable>(with selectKey: (Element) -> Key) -> [Key: Element] {
reduce(into: [:]) { $0[selectKey($1)] = $1 }
}
}
// let arr = [Person(id: 1, name: "Alan")]
// arr.toDictionary { $0.id }
// ==
// [1: Person(id: 1, name: "Alan")]

Swift: match two arrays into one array

I have two arrays
array1 = ["Fri","Sat","Sun"]
array2 = ["5","6","7"]
Now I want to create a newArray: ["Fri5", "Sat6", "Sun7"]. How to make it? Thanks in advance.
For a functional approach, use zip and map:
let array1 = ["Fri","Sat","Sun"]
let array2 = ["5","6","7"]
let result = zip(array1, array2).map { $0 + $1 }
print(result)
Output:
["Fri5", "Sat6", "Sun7"]
zip creates a sequence of tuples [("Fri", "5"), ("Sat", "6"), ("Sun", "7")] and map then creates the final array by taking each tuple and combining the two Strings into a single String.
You can also try with this:
var array1 = ["Fri","Sat","Sun"]
var array2 = ["5","6","7"]
var array3 = array1.map{ $0 + (array2[array1.indexOf($0)!]) } // ["Fri5", "Sat6", "Sun7"]
With Swift 5 you can use one of the five following Playground sample codes in order to solve your problem.
#1. Using sequence(state:next:)
let array0 = ["Fri", "Sat", "Sun"]
let array1 = ["5", "6", "7"]
let unfoldSequence = sequence(state: (array0.makeIterator(), array1.makeIterator()), next: { (iterators) -> String? in
guard let string0 = iterators.0.next(), let string1 = iterators.1.next() else { return nil }
return string0 + string1
})
let array = Array(unfoldSequence)
print(array) // prints: ["Fri5", "Sat6", "Sun7"]
#2. Using zip(_:_:) and map(_:)
let array0 = ["Fri", "Sat", "Sun"]
let array1 = ["5", "6", "7"]
let array = zip(array0, array1).map({ $0 + $1 })
print(array) // prints: ["Fri5", "Sat6", "Sun7"]
#3. Using zip(_:_:) and reduce(into:_:)
let array0 = ["Fri", "Sat", "Sun"]
let array1 = ["5", "6", "7"]
let array = zip(array0, array1).reduce(into: [String](), { (result, tuple) in
result.append(tuple.0 + tuple.1)
})
print(array) // prints: ["Fri5", "Sat6", "Sun7"]
#4. Using AnyIterator
let array0 = ["Fri", "Sat", "Sun"]
let array1 = ["5", "6", "7"]
var iterator0 = array0.makeIterator()
var iterator1 = array1.makeIterator()
let anyIterator = AnyIterator<String> {
guard let string0 = iterator0.next(), let string1 = iterator1.next() else { return nil }
return string0 + string1
}
let array = Array(anyIterator)
print(array) // prints: ["Fri5", "Sat6", "Sun7"]
#5. Creating an object that conforms to Sequence and IteratorProtocol protocols
struct StringArrayConcatenatingSequence: Sequence, IteratorProtocol {
let firstArray: [String]
let secondArray: [String]
private var index = 0
init(firstArray: [String], secondArray: [String]) {
self.firstArray = firstArray
self.secondArray = secondArray
}
mutating func next() -> String? {
guard index < Swift.min(firstArray.count, secondArray.count) else { return nil }
defer { index += 1 }
return firstArray[index] + secondArray[index]
}
}
let array0 = ["Fri", "Sat", "Sun"]
let array1 = ["5", "6", "7"]
let sequence = StringArrayConcatenatingSequence(firstArray: array0, secondArray: array1)
let array = Array(sequence)
print(array) // prints: ["Fri5", "Sat6", "Sun7"]

How to reverse array in Swift without using ".reverse()"?

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"]
Swift 3:
var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames : [String] = Array(names.reversed())
print(reversedNames) // ["Asus", "Lenovo", "Sony", "Microsoft", "Apple"]
Here is #Abhinav 's answer translated to Swift 2.2 :
var names: [String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames = [String]()
for arrayIndex in (names.count - 1).stride(through: 0, by: -1) {
reversedNames.append(names[arrayIndex])
}
Using this code shouldn't give you any errors or warnings about the use deprecated of C-style for-loops or the use of --.
Swift 3 - Current:
let names: [String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames = [String]()
for arrayIndex in stride(from: names.count - 1, through: 0, by: -1) {
reversedNames.append(names[arrayIndex])
}
Alternatively, you could loop through normally and subtract each time:
let names = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
let totalIndices = names.count - 1 // We get this value one time instead of once per iteration.
var reversedNames = [String]()
for arrayIndex in 0...totalIndices {
reversedNames.append(names[totalIndices - arrayIndex])
}
Do you really need a for loop? If not, you can use reduce.
I guess that this is the shortest way to achieve it without reversed() method (Swift 3.0.1):
["Apple", "Microsoft", "Sony", "Lenovo", "Asus"].reduce([],{ [$1] + $0 })
Only need to make (names.count/2) passes through the array. No need to declare temporary variable, when doing the swap...it's implicit.
var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
let count = names.count
for i in 0..<count/2 {
(names[i],names[count - i - 1]) = (names[count - i - 1],names[i])
}
// Yields: ["Asus", "Lenovo", "Sony", "Microsoft", "Apple"]
There's also stride to generate a reversed index:
let names = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversed = [String]()
for index in (names.count - 1).stride(to: -1, by: -1) {
reversed.append(names[index])
}
It also works well with map:
let reversed = (names.count - 1).stride(to: -1, by: -1).map { names[$0] }
Note: stride starts its index at 1, not at 0, contrary to other Swift sequences.
However, to anyone reading this in the future: use .reverse() instead to actually reverse an array, it's the intended way.
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
}
Here you go:
var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames = [String]()
for var arrayIndex = names.count - 1 ; arrayIndex >= 0 ; arrayIndex-- {
reversedNames.append(names[arrayIndex])
}
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)
Here is the most simpler way.
let names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames = [String]()
for name in names {
reversedNames.insert(name, at: 0)
}
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
}
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: &👈)
}
}
}
Like this, maybe:
names = names.enumerate().map() { ($0.index, $0.element) }.sort() { $0.0 > $1.0 }.map() { $0.1 }
Oh, wait.. I have to use for loop, right? Then like this probably:
for (index, name) in names.enumerate().map({($0.index, $0.element)}).sort({$0.0 > $1.0}).map({$0.1}).enumerate() {
names[index] = name
}
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)
This will work with any sized array.
import Cocoa
var names:[String] = [ "A", "B", "C", "D", "E","F"]
var c = names.count - 1
for i in 0...(c/2-1) { swap(&names[i],&names[c-i]) }
print(names)
Here is how I did it and there is no warning for Swift 3
let names = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames = [String]()
for name in names.enumerate() {
let newIndex = names.count - 1 - name.index
reversedNames.append(names[newIndex])
}
or just simply
reversedNames = names.reverse()
Here the code for swift 3
let array = ["IOS A", "IOS B", "IOS C"]
for item in array.reversed() {
print("Found \(item)")
}
func reverse(array: inout [String]) {
if array.isEmpty { return }
var f = array.startIndex
var l = array.index(before: array.endIndex)
while f < l {
swap(array: &array, f, l)
array.formIndex(after: &f)
array.formIndex(before: &l)
}
}
private func swap( array: inout [String], _ i: Int, _ j: Int) {
guard i != j else { return }
let tmp = array[i]
array[i] = array[j]
array[j] = tmp
}
Or you can write extension of course
var rArray : [Int] = [2,5,6,8,3,8,9,10]
var ReArray = [Int]()
var a : Int = 1
func reversed (_ array: [Int]) -> [Int] {
for i in array {
ReArray.append(array[array.count-a])
a += 1
}
rArray = ReArray
return rArray
}
reversed(rArray)
print(rArray)
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 :)
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)
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
}
Do it like this for reversed sorting.
let unsortedArray: [String] = ["X", "B", "M", "P", "Z"]
// reverse sorting
let reversedArray = unsortedArray.sorted() {$0 > $1}
print(reversedArray) // ["Z", "X", "P", "M", "B"]
I like simple codes.
var names:[String] = ["Apple", "Microsoft", "Sony", "Lenovo", "Asus"]
var reversedNames = [""]
for name in names {
reversedNames.insert(name, at: 0)
}
print(reversedNames)
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"]"
You can use the swift3 document:
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
let reversedNames = names.sorted(by: >)
// reversedNames is equal to:
// ["Ewa", "Daniella", "Chris", "Barry", "Alex"]

Resources