Search This Blog

Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Tuesday, October 17, 2017

Find most common word in array of string using swift

Following task - find most common word in array of string.

Algorithm
  1. Create and fill up dictionary of word frequency. Count for each word in array
  2. Find key with maximum value and return this key.

Let's get implemented this algorithm.
let array = ["This", "Test", "Swift", "Never", "Swift", "Test", "Swift"]

func getMostCommonWord(array: [String]) -> String {
    
    var dict = [String: Int]()
    
    for word in array {
        if let count = dict[word] {
            dict[word] = count + 1
        } else {
            dict[word] = 1
        }
    }
    
    var mostCommonWord = ""
    for key in dict.keys {
        if mostCommonWord == "" {
            mostCommonWord = key
        }
        if let nextCount = dict[key], let prevCount = dict[mostCommonWord] {
            if nextCount > prevCount {
                mostCommonWord = key
            }
        }
    }
    
    return mostCommonWord
}

print(getMostCommonWord(array: array))
// Swift

How we can improve this algorithm. Using High-ordered functions. 

We can sort dictionary. As result of sorting dictionary we get descendingly sorted array of tuples. In this array of tuples first element contain key with maximum count. After that we can get first tuple and get key variable from it.
let sortedTuples = dict.sorted { $0.1 > $1.1}
print(sortedTuples)
// [(key: "Swift", value: 3), (key: "Test", value: 2), (key: "This", value: 1), (key: "Never", value: 1)]
print(sortedTuples.first!)
// (key: "Swift", value: 3)

So now we can rewrite our function.
func getMostCommonWordUsingMap(array: [String]) -> String {
    
    var dict = [String: Int]()
    
    for word in array {
        if let count = dict[word] {
            dict[word] = count + 1
        } else {
            dict[word] = 1
        }
    }
    
    var mostCommonWord = ""
    
    if let first = (dict.sorted { $0.1 > $1.1}).first {
        mostCommonWord = first.key
    }
    
    return mostCommonWord
}

print(getMostCommonWordUsingMap(array: array))
// Swift

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