DEV Community

Discussion on: Javascript Sock Merchant Challenge - Solution 1

Collapse
 
mindmath profile image
Scale to zero • Edited

Ady
Thanks for this...
Its helping me understand the order of computation better

So overall it is O(nlogn) * O(n)

// Complete the sockMerchant function below.
func sockMerchant(n: Int, ar: [Int]) -> Int {
var pair = 0
let sortedArr = ar.sorted() // O(nlogn)
var i=0
while (i<(sortedArr.count-1)) { // O(nlogn * n)
if sortedArr[i]==sortedArr[i+1]{
pair += 1
i += 1
}
i += 1
}
return pair
}

sockMerchant(n: 9, ar: [10, 20, 20, 10, 10, 30, 50, 10, 20])