We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Swift, Here is my idea. It is easy to understand, however it is too long
funcgemstones(arr:[String])->Int{varuniqueData=Set<String>()varresult=0guardletfirstIndex=arr.firstelse{returnresult}firstIndex.map{uniqueData.insert(String($0))}foriin1..<arr.count{vartemp=Set<String>()arr[i].map{temp.insert(String($0))}print("uniqueData \(uniqueData) and temp is \(temp)")print("intersection \( uniqueData.intersection(temp))")varintersection=uniqueData.intersection(temp)result=intersection.countprint("result ",result)uniqueData=[]uniqueData=intersection}returnresult}
Then, I refactor the code using formIntersection. we don't need the unnecessary variables
Gemstones
You are viewing a single comment's thread. Return to all comments →
Swift, Here is my idea. It is easy to understand, however it is too long
Then, I refactor the code using formIntersection. we don't need the unnecessary variables