Swift - GCD based map
Code\nswift []\nclass Solution {\n    //if nums[i] * nums[j] % k == 0, \n    //then GCD(nums[i], k) * GCD(nums[j], k) is also div by k\n    func countPairs(_ nu
# Code\n```swift []\nclass Solution {\n    //if nums[i] * nums[j] % k == 0, \n    //then GCD(nums[i], k) * GCD(nums[j], k) is also div by k\n    func countPairs(_ nums: [Int], _ k: Int) -> Int {\n        var dict = [Int: Int]() //how many times we found GCD(x) in array\n        var count = 0\n\n        for num in nums {\n            let gcd = findgcd(num, k)\n\n            for key in dict.keys {\n                if (key * gcd) % k == 0 {\n                    count += dict[key]!\n                }\n            }\n            dict[gcd, default: 0] += 1\n        }\n\n        return count\n    }\n\n    private func findgcd(_ a: Int, _ b: Int) -> Int {\n        if a < b { return findgcd(b, a) }\n        if a % b == 0 { return b }\n        return findgcd(b, a % b)\n    }\n}\n```
['Swift']