Programming Algo Interview Question in SWIFT


Programming with AppCodeZip
  • Find min / max value in Swift Array.


func minmax(arr:[Int])->(max:Int,min:Int){
    var minValue = arr[0]
    var maxValue = arr[0]
    for item in 0..<arr.count {
        if arr[item] > maxValue {
            maxValue = arr[item]
        }
        if arr[item] < minValue {
            minValue = arr[item]
        }
    }
    return(maxValue,minValue) // (max 9, min 5)
}

//Func Call

minmax(arr: [5,6,7,8,9]) 

  • Find the second largest number in an array use swift language.


func scandLargest(arr:[Int])->Int{
    var max = 0
    var secandlar = 0
    for item in 0..<arr.count {
        if arr[item] > max {
            secandlar  = max
           max = arr[item]
        }
        else if arr[item] > secandlar{
            secandlar = arr[item]
        }
    }
    return secandlar
}

//Func Call
let value = scandLargest(arr: [7,8,17,16])
print("Secand Largest Number ====================> \(value)") // 16

  • How to find the highest value in an array.


let numbers = [1, 2, 3, 4, 5]
let max = numbers.max()  //5

  • Program to count the total number of characters in a string.


func countCharacter(str:String,character:String)->Int{
    return str.count - str.replacingOccurrences(of: character, with: "").count
}

countCharacter(str: "My Name is sushil Kumar", character: "i") // 2


  • How to count element frequencies in an array


let array = [4, 23, 97, 97, 97, 23]
var dictionary:[Int:Int] = [:]
array.map{
    if let val:Int = dictionary[$0]{
       dictionary[$0] = val + 1

    }else{
         dictionary[$0] = 1
    }

}

print(dictionary)   // [97: 3, 4: 1, 23: 2]

OR

let array = [4, 23, 97, 97, 97, 23]
var dictionary:[Int:Int] = [:]

for item in array {
   if let val:Int = dictionary[item]{
       dictionary[item] = val + 1
        
    }else{
         dictionary[item] = 1
    }
}
print(dictionary)

OR

let array = ["Hello", "RAM", "Hello","AppCodeZip", "Hello","RAM","AppCodeZip","AppCodeZip","AppCodeZip",]

var dictionary:[String:Int] = [:]

for item in array {
   if let val:Int = dictionary[item]{
       dictionary[item] = val + 1
        
    }else{
         dictionary[item] = 1
    }
}
print(dictionary) //["RAM": 2, "Hello": 3, "AppCodeZip": 4]


  • Removing duplicate elements from an array in Swift


let arrayOfInts = [2, 2, 4, 4]
let uniqNo = Array(Set(arrayOfInts))  //[2,4]

OR

extension Array where Element:Equatable{
    func removeD() -> [Element] {
        var res = [Element]()
        for item in self {
            if res.contains(item) == false {
                res.append(item)
            }
        }
        return res
        
    }
}
let arrayOfInts = [2, 2, 4, 4]

print(arrayOfInts.removeD()) // Prints: [2, 4]

OR 

func unique<S : Sequence, T : Hashable>(source: S) -> [T] where S.Iterator.Element == T {
    var buffer = [T]()
    var added = Set<T>()
    for elem in source {
        if !added.contains(elem) {
            buffer.append(elem)
            added.insert(elem)
        }
    }
    return buffer
}

let vals = [1, 4, 2, 2, 6, 24, 15, 2, 60, 15, 6]
let uniqueVals = unique(source: vals) // [1, 4, 2, 6, 24, 15, 60]


  • How to reverse a string in Swift


var name = "Hi sushil how r you!"
var reverse = ""
for item in name
{
    reverse = item.description + reverse

}
print(reverse)  // !uoy r woh lihsus iH

OR

var name = "Hi sushil how r you!"
var reverse = ""
for item in name
{
    reverse.insert(item, at: reverse.startIndex)
}
print(reverse)  // !uoy r woh lihsus iH



Post a Comment

1 Comments

  1. micro hair trimmer in the tin - iTanium-arts.com
    micro titanium exhaust tubing hair trimmer in the tin toaks titanium 750ml pot - iTanium-arts.com. - Made in South titanium curling iron Africa. $0.25 titanium drill bit set - $19.50. Rating: titanium ore 4.9 · ‎1,000 reviews · ‎$19.50 · ‎In stock

    ReplyDelete