Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2 count duplicate in array

Could you please help me to simplify the flow below? the scope is to count how many time each element is duplicate within the given array.

var test = [1,2,3,4,5,6,7,8,5,9,3,3,9,9,9]

var testCount = [Int:Int]()

for curr in test {
    if let x = testCount[curr] {
        testCount[curr] = x  + 1
        continue;
    }
    testCount[curr] = 1
}

print(testCount)

I'm working on it in order to have something like:

test.map_duplicate() or map_duplicate(test)
like image 940
Attilio Avatar asked Feb 28 '26 17:02

Attilio


2 Answers

extension Array where Element: Hashable {

    func mergeDuplicates() -> [Element: Int] {
        var result = [Element: Int]()

        self.forEach({ result[$0] = result[$0] ?? 0 + 1 })

        return result
    }

}

var test = [1,2,3,4,5,6,7,8,5,9,3,3,9,9,9]

let testCount = test.mergeDuplicates()

print(testCount)

The code above provides an extension to any Array type that holds Hashable elements (anything else is not eligible to be the key of a dictionary). Method mergeDuplicates produces the dictionary you want. In its implementation, forEach() executes a closure over every element in the array (this is typically more straightforward and transparent alternative to old-fashioned for-loop). Then on each iteration we check whether there is already existing value in the dictionary with a running count (and use nil-coalescing operator ?? to handle cases when a particular value was not yet met before, hence it's zero). Then increment, and store (back) into the dictionary.

like image 58
0x416e746f6e Avatar answered Mar 03 '26 05:03

0x416e746f6e


You could extend SequenceType to have a function freq which does exactly what you need here.

extension SequenceType where Self.Generator.Element: Hashable {
    func freq() -> [Self.Generator.Element: Int] {
        return reduce([:]) { (var accu: [Self.Generator.Element: Int], element) in
            accu[element] = accu[element]?.successor() ?? 1
            return accu
        }
    }
}
like image 45
tskulbru Avatar answered Mar 03 '26 07:03

tskulbru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!