
🧑💻iOS Engineer by profession with 8+ years of experience. Developing iOS apps since 2014. Love to write and comment on Swift, SwiftUI, iOS app sec/pentest and iOS related stuff.
Search for a command to run...

🧑💻iOS Engineer by profession with 8+ years of experience. Developing iOS apps since 2014. Love to write and comment on Swift, SwiftUI, iOS app sec/pentest and iOS related stuff.
No comments yet. Be the first to comment.
In this series I will share the Data structures and Algorithms problems with approach and solution using Swift language.
Background When I was beginner or some where around 1-2 years of experience I usually developed the app in very monolithic style code base, where I have single repo which contains folders and files depending on kind on design pattern used . For bette...

Hello, Visitor This is Saurabh Mishra , Software engineer from India🇮🇳 . Developing iOS apps since 2014, currently associated with Adidas👟 as Senior iOS engineer. Worked on multiple domains like Store Management, Retail Banking, Fleet Management, ...

It's been a more than a year I am working with SwiftUI professionally. From very starting phase of app me and my time followed MMVM like Bible into the code base and today I want to share the road block we hit every time when we were enforcing MVVM w...


Random thought/BruteForce
Initially looking at the problem there might be multiple solution came into your mind. My initial thought was kind of linear search for finding the result/appropriate number from array.
But that will not be the best solution as you will keep iterating to find an element from array , and this is the thing where we can optimise our solution i.e Searching. So if somehow we can directly get to know that certain element is there in array or not our job will be very easy and code also looks good at the same time.
To solve that we need to somehow use Hashmap or Dictionary. So lets see the solution now.
class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var dict = [Int: Int]()
for (index, item) in nums.enumerated() {
let value = target - item
//this piece of code is storing and searching element at the same time
// previousIndex != index -> important condition to check
if let previousIndex = dict[value], previousIndex != index {
return [previousIndex, index]
} else {
dict[item] = index
}
}
return []
}
}
What if problem changed a little bit and now inspite of printing index , output should contains those numbers.
Let's see the solution for that , very minor change and job done.
class Solution {
func twoSum(_ array: inout [Int], _ targetSum: Int) -> [Int] {
var dict = [Int: Int]()
for (index, item) in array.enumerated() {
let numberToFind = targetSum - item
if let previousIndex = dict[numberToFind], previousIndex != index {
// we got the number
return [numberToFind, item]
} else {
dict[item] = index
}
}
return []
}
}
Do not try to use inbuilt functions of Swift like isContains, that will be little problematic and also not a optimise solution.