# Top 50 Coding Interview Questions with Solution | Swift data structure | iOS - Part-1

iOS Swift Programming DSA Interview Question

Swift LeetCode Problem

1- Shuffle String

Example 1:
Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
Output: "leetcode"
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.

Example 2:
Input: s = "abc", indices = [0,1,2]
Output: "abc"
Explanation: After shuffling, each character remains in its position.

```// MARK : 1528. Shuffle String
func restoreString(_ s: String, _ indices: [Int]) -> String {
var arrText = Array(s)
var result = String()
var count = 0

while count <= arrText.count - 1 {
for i in 0...arrText.count - 1 {
if indices[i] == count {
result += String(arrText[i])
}
}
count += 1
}
return result
}

print(restoreString("codeleet", [4,5,6,7,0,2,1,3]))
//leetcode
```

2- Two Sum Problem return index
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

Input: nums = [2,7,11,15, 5, 31, target = 20
Output: [3,4]
Output: Because nums  + nums  == 9, we return [0, 1].

```func twosum (_ nums: [Int], target: Int) -> [Int] {
var dict = [Int: Int]()
for (index, number) in nums.enumerated () {
dict [number] = index
}
print (dict) //[4: 2, 3: 0, 2: 1]

for (index, num) in nums.enumerated () {
if let otherIndex = dict[target - num], otherIndex != index {
return [index, otherIndex]
}
}
return [0, 0]
}

let nums = [3,2,4]
print(twosum(nums, target: 6))
//[1, 2]
```

3- Two Sum Problem:

```func sumProblemWithDict(arr:[Int],target:Int) -> [Int] {
var tempDict = [Int:Int]()
for index in 0..<arr.count{
let diff = target-arr[index]
if let getIndex = tempDict[arr[index]] {
return[index,getIndex]
}else{
tempDict[diff] = index
}
}
return[]
}
print(sumProblemWithDict(arr: TempArray, target: 9))//[2, 0]
```

4- Two Sum Problem:

```func sumProblem(arr:[Int],target:Int) -> [Int] {
for index in 0..<arr.count{
for index1 in index+1..<arr.count{
if arr[index] + arr[index1] == target{
return[index,index1]
}
}
}
return[]
}
print(sumProblem(arr: TempArray, target: 9))//[0, 2]
```

5- Two Sum Problem return value:

```var inputArray = [2,1,8,9]
var target = 10
func firstApproch(inputArray:[Int],target:Int){

for (index1,item1) in inputArray.enumerated(){
for (index2,item2) in inputArray.enumerated(){
if index1 != index2 && item1 + item2 == target {
print("\(item1),\(item2)")

}
}
}
}
firstApproch(inputArray: inputArray, target: target)
//2,8
//1,9
//8,2
//9,1
```

6- First Charcter in Capital letter:
Input:  "first_values_kml"
Output: "firstValuesKml"

```func firstCharacterUpperCase(sentenceToCap:String) -> String {
var breakupSentence = sentenceToCap.components(separatedBy: "_")

var newSentence = ""

for wordInSentence  in breakupSentence{
if wordInSentence == breakupSentence {
newSentence = "\(wordInSentence)"
}else{
newSentence = "\(newSentence)\(wordInSentence.capitalized)"
}
}

return newSentence
}
print(firstCharacterUpperCase(sentenceToCap: "first_values_kml"))
//firstValuesKml
```

7- REVERSE WORDS OF A SENTENCE

```//MARK: REVERSE WORDS OF A SENTENCE--
func reverseStr(str:String) -> String {
var getStr = str
var newWord = ""
var tempStr = ""
for char in getStr {
tempStr = tempStr + "\(char)"
if char == " " {
newWord = tempStr + newWord
tempStr = ""
}
}
return tempStr + " " + newWord
}
print(reverseStr(str: "Allign words in reverse order"))
//order reverse in words Allign
```

8- Remove Duplicate Letters
Input: s = "programming"
Output: "progamin"

```func removeDuplicateLetters(_ inputString: String) -> String {
var result = ""
var seen = Set<Character>()

for char in inputString {
if !seen.contains(char) {
seen.insert(char)
result.append(char)
}
}

return result
}

let input = "programming"
let output = removeDuplicateLetters(input)
print(output) // Output will be "progamin"
```

In this example, the removeDuplicateLetters function takes an input string and iterates through its characters.
It uses a Set called seen to keep track of the characters that have been encountered so far. If a character is not in the seen set, it means it's unique, so it is added to the result string, and it's also added to the seen set to mark it as seen.
This process ensures that only the first occurrence of each character is included in the result string, effectively removing duplicates.

9- Remove Vowels from a String
To remove vowels from a string in Swift, you can create a function that iterates through the characters of the string and builds a new string without vowels.
You can define a set of vowel characters and check if each character in the input string is not in that set. Here's an example:

```func removeVowels(_ inputString: String) -> String {
let vowels: Set<Character> = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
var result = ""

for char in inputString {
if !vowels.contains(char) {
result.append(char)
}
}

return result
}

let input = "Hello, World!"
let output = removeVowels(input)
print(output) // Output will be "Hll, Wrld!"
```

In this example, the removeVowels function takes an input string and iterates through its characters. It checks whether each character is in the vowels set and appends it to the result string only if it's not a vowel.
The result is a new string with all the vowels removed.

10- Second Largest Digit in a String (LeetCode #1796)
To find the second-largest digit in a string in Swift, you can create a function that iterates through the characters of the string, identifies digits, and keeps track of the largest and second-largest digits encountered.
Here's an example:

```func secondLargestDigit(_ s: String) -> Int {
var largest = -1
var secondLargest = -1

for char in s {
if let digit = Int(String(char)) {
if digit > largest {
secondLargest = largest
largest = digit
} else if digit < largest && digit > secondLargest {
secondLargest = digit
}
}
}

return secondLargest
}

let input = "abc12345de67"
let result = secondLargestDigit(input)
print(result) // Output will be 6
```

In this example:
We initialize two variables, largest and secondLargest, to -1 initially because we assume there are no digits in the string at the start.
We iterate through each character in the input string s.
For each character, we use Int(String(char)) to check if it's a digit. If it is, we compare it to the current largest digit.

If the digit is larger than the current largest, we update secondLargest to be the previous largest, and largest becomes the current digit.
If the digit is smaller than the current largest and larger than the current secondLargest, we update secondLargest to be the current digit.
Finally, we return secondLargest, which will be the second-largest digit found in the string.
In the provided example, the input string "abc12345de67" contains digits, and the second-largest digit is 6.