From b144b769e3eb800b337f7ba3a2cf01b6165f439a Mon Sep 17 00:00:00 2001 From: Soap Date: Mon, 5 Aug 2019 20:22:00 -0700 Subject: [PATCH] [Array] Refactor solution to Intersection of Two Arrays II --- Array/IntersectionTwoArraysII.swift | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Array/IntersectionTwoArraysII.swift b/Array/IntersectionTwoArraysII.swift index 739caeda..a5ec9159 100644 --- a/Array/IntersectionTwoArraysII.swift +++ b/Array/IntersectionTwoArraysII.swift @@ -10,20 +10,16 @@ class IntersectionTwoArraysII { func intersect(_ nums1: [Int], _ nums2: [Int]) -> [Int] { - var frequencies = Dictionary(nums1.map { ($0, 1) } , uniquingKeysWith: +) + var numsFreq = Dictionary(nums1.map { ($0, 1) }, uniquingKeysWith: +) var res = [Int]() for num in nums2 { - guard let frequent = frequencies[num] else { - continue - } - - if frequent > 0 { - frequencies[num]! = frequent - 1 + if let freq = numsFreq[num], freq > 0 { res.append(num) + numsFreq[num] = freq - 1 } } return res } -} \ No newline at end of file +}