'Swift 4 - How to return a count of duplicate values from an array? [duplicate]

I have an array with multiple values (Doubles), many of which are duplicates. I'd like to return or print a list of all unique values, along with a count of how many times a given value appears in the array. I'm pretty new to Swift and I've tried a couple of different things but I'm unsure of the best way to accomplish this.

Something like this: [65.0, 65.0, 65.0, 55.5, 55.5, 30.25, 30.25, 27.5]

Would print (for example): "3 at 65.0, 2 at 55.5, 2 at 30.25, 1 at 27.5."

I'm not really concerned about the output as much as the method to accomplish this.

Thanks!



Solution 1:[1]

You can enumerate through the array and add the values to the dictionary.

var array: [CGFloat] =  [65.0, 65.0, 65.0, 55.5, 55.5, 30.25, 30.25, 27.5]
var dictionary = [CGFloat: Int]()

for item in array {
   dictionary[item] = dictionary[item] ?? 0 + 1
}

print(dictionary)

or you can do foreach on array:

array.forEach { (item) in
  dictionary[item] = dictionary[item] ?? 0 + 1
}

print(dictionary)

or as @rmaddy said:

var set: NSCountedSet =  [65.0, 65.0, 65.0, 55.5, 55.5, 30.25, 30.25, 27.5]
var dictionary = [Float: Int]()
set.forEach { (item) in
  dictionary[item as! Float] = set.count(for: item)
}

print(dictionary)

Solution 2:[2]

As @rmaddy already commented you can use Foundation NSCountedSet as follow:

import Foundation // or iOS UIKit or macOS Cocoa

let values = [65.0, 65.0, 65.0, 55.5, 55.5, 30.25, 30.25, 27.5]
let countedSet = NSCountedSet(array: values)
print(countedSet.count(for: 65.0))   // 3
for value in countedSet {
    print("Element:", value, "count:", countedSet.count(for: value))
}

Xcode 11 • Swift 5.1

You can also extend NSCountedSet to return an array of tuples or a dictionary:

extension NSCountedSet {
    var occurences: [(object: Any, count: Int)] { map { ($0, count(for: $0))} }
    var dictionary: [AnyHashable: Int] {
        reduce(into: [:]) {
            guard let key = $1 as? AnyHashable else { return }
            $0[key] = count(for: key)
        }
    }
}

let values = [65.0, 65.0, 65.0, 55.5, 55.5, 30.25, 30.25, 27.5]
let countedSet = NSCountedSet(array: values)
for (key, value) in countedSet.dictionary {
    print("Element:", key, "count:", value)
}

For a Swift native solution we can extend Sequence constraining its elements to Hashable:

extension Sequence where Element: Hashable {
    var frequency: [Element: Int] { reduce(into: [:]) { $0[$1, default: 0] += 1 } }
}

let values = [65.0, 65.0, 65.0, 55.5, 55.5, 30.25, 30.25, 27.5]
let frequency = values.frequency
frequency[65] // 3
for (key, value) in frequency {
    print("Element:", key, "count:", value)
}

Those will print

Element: 27.5 count: 1
Element: 30.25 count: 2
Element: 55.5 count: 2
Element: 65 count: 3

If you have a collection of custom structure we can create a generic method and make use of keypath syntax as follow:

extension Sequence {
    func sum<T: Hashable>(of property: (Element) -> T) -> [T: Int] {
        reduce(into: [:]) { $0[property($1), default: 0] += 1 }
    }
    func sum<T: Hashable, A: AdditiveArithmetic>(of property: (Element) -> T, by adding: (Element) -> A) -> [T: A] {
        reduce(into: [:]) { $0[property($1), default: .zero] += adding($1) }
    }
}

Usage:

struct Product {
    let id: String
    let name: String
    let quantity: Int
    let price: Decimal
    let total: Decimal
}

let products = [
    ("1", "iPhone",      2,  Decimal(string: "499.99")!,  Decimal(string: "999.98")!),
    ("2", "MacBook Pro", 1, Decimal(string: "2499.99")!, Decimal(string: "2499.99")!),
    ("3", "iPhone",      3, Decimal(string: "1199.99")!, Decimal(string: "3599.97")!),
    ("4", "iPhone",      1,  Decimal(string: "999.99")!,  Decimal(string: "999.99")!),
    ("5", "MacBook Pro", 2, Decimal(string: "1499.99")!, Decimal(string: "2999.98")!)
].map(Product.init)
 
let sum1 = products.sum(of: \.name)
sum1["iPhone"]       // 3
sum1["MacBook Pro"]  // 2

let sum2 = products.sum(of: \.name, by: \.quantity)
sum2["iPhone"]       // 6
sum2["MacBook Pro"]  // 3

let sum3 = products.sum(of: \.name, by: \.total)
sum3["iPhone"]       // 5599.94
sum3["MacBook Pro"]  // 5499.97

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2