Last active
March 14, 2019 14:24
-
-
Save bilawal-liaqat/cdd1a7b6f67161637a3cb4a5eea6b0fc to your computer and use it in GitHub Desktop.
Swift Array interview questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func removeDuplicate(_ list : [Int]) -> Array<Int>{ | |
var result = [Int]() | |
var previous = list[0] | |
result.append(previous) | |
for i in 1 ..< list.count { | |
let check = list[i] | |
if previous != check{ | |
result.append(check) | |
} | |
previous = check | |
} | |
return result | |
} | |
func largestSmallest(_ list : [Int]){ | |
var larget = 0 | |
var smallest = 0 | |
for value in list{ | |
if value > larget { | |
larget = value | |
} | |
if value < smallest { | |
smallest = value | |
} | |
} | |
print("Largest Number is \(larget)") | |
print("Smallest Number is \(smallest)") | |
} | |
func printPair(_ list : [Int], sum : Int){ | |
for i in 0 ..< list.count { | |
let first = list[i] | |
for j in i+1 ..< list.count { | |
let second = list [j] | |
if ((first + second) == sum ){ | |
print ("\(i+1)- pair is \(first) , \(second) ") | |
} | |
} | |
} | |
} | |
func Fib(_ number : Int ) -> Int{ | |
if number <= 1 { | |
return 1 | |
} | |
return Fib(number - 1) + Fib(number-2); | |
} | |
func factorial(_ num : Int) -> Double { | |
// var fact = 1 | |
if num <= 1 { | |
return 1 | |
} | |
return Double(num) * factorial(num-1) | |
} | |
print(factorial(11)) | |
var listCheck = [1,2,3,4,5,8,7,6,4] | |
printPair(listCheck , sum : 7) | |
print (Fib(5)) | |
// largestSmallest(listCheck) | |
//listCheck.sort() | |
//print(removeDuplicate(listCheck)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment