Data Structure Program in Swift Part 1
- Longest Subarray length from the given Value
var arr = [1,2,3,5,7,3,10]
let k = 10
func longestArray(arr:[Int],k:Int) -> Int {
var longestSum = 0
for i in 0..<arr.count {
var sum = 0
for j in i..<arr.count {
sum += arr[j]
if sum == k {
longestSum = max(longestSum, j-i+1)
}
}
}
return longestSum
}
print(longestArray(arr: arr, k: 10))
The output of this program will be 3 because the longest subarray for sum 5 is [2,3,5], In the above program I used two loops the first loop started from 0 and the second loop started from I.
In the second loop, I increased the sum by array value and compair with K if it is equal to K then I found the longest value between the previous longest and the current by using the max function.
2. Find the number that appears ones.
var arr = [1,1,4,1]
func singleNumber(_ nums: [Int]) -> Int {
var arrDic = [Int:Int]()
for item in nums {
arrDic[item,default: 0] += 1
}
var singleElement = 0
for item in nums {
guard let count = arrDic[item] , count == 1 else {continue}
singleElement = item
break
}
return singleElement
}
print(singleNumber(arr))
The Output of this program will be 4 because 1 is coming four times but 4 is coming one time. In the above program, I used two loops in the first loop I created a dictionary that contained the frequency of the array element and in the second loop I checked which element had one frequency after getting this I broke the loop.
3. Remove Duplicate Elements from array
var arr = [1,1,2]
func removeDuplicate(arr:[Int]) -> [Int] {
var arrDic = [Int:Int]()
for item in arr {
arrDic[item,default: 0] += 1
}
var nonDuplicateArr = [Int]()
for item in arr {
guard let count = arrDic[item] , count > 0 else { continue }
nonDuplicateArr.append(item)
arrDic[item] = 0
}
return nonDuplicateArr
}
print(removeDuplicate(arr: arr))
The output of this program will be [1,2] because I removed duplicate item 1, In the above example I have created a dictionary that contains the frequency of each element. In the next loop, I have made a new array that contains only single frequency element
4. Left Rotate the Array by k th place k will be any number
var arr = [1,1,2,-100]
func rotate(_ nums: inout [Int], _ k: Int) {
let n = nums.count
var i = 0
while i < k {
let temp = nums[n-1]
nums.remove(at: n-1)
nums.insert(temp, at: 0)
i += 1
}
}
rotate(&arr, 2)
print(arr)
The output of this program will [2,-100,1,1], In the above program I have created a while loop. that will execute till the value of I is greater than or equal to K.
Suppose k is 2 and I is 0 then this loop will execute 2 times. in this loop, I just removed the value from the end and placed it at the start.
5. Move Zero at the end of the loop
var arr = [0,1,0,-100]
func moveZeroes(_ nums: inout [Int]) {
for i in nums.indices.reversed() where nums[i] == 0 {
nums.remove(at: i)
nums.append(0)
}
}
moveZeroes(&arr)
print(arr)
The output of the above program will be [1,-100,0,0], In the above program I have used a where clause for 0 elements in the array, Which means this loop will only execute for 0.
In this loop, I removed 0 and appended it at the end of the array.
Thank you for reading. Please clap or follow if you liked this!