Skip to content

Instantly share code, notes, and snippets.

View raybellis's full-sized avatar

Ray Bellis raybellis

  • Oxfordshire, England
View GitHub Profile
@raybellis
raybellis / ArrayModalValue.swift
Created November 16, 2022 22:39
A Swift extension to find the most common (modal) value among an array of (hashable) values
extension Array where Element : Hashable {
func modalValue() -> Self.Element? {
// populate a dictionary of element vs count
var dict: [Self.Element : Int] = [:]
self.forEach { v in
if let count = dict[v] {
dict[v] = count + 1
} else {