From d00bca4e12a8133d4816c125760e383e31ca8c11 Mon Sep 17 00:00:00 2001 From: Arthur Guiot Date: Thu, 8 Sep 2022 17:17:42 -0400 Subject: [PATCH] Added mode to statistics object --- Sources/Euler/Statistics/Statistics.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sources/Euler/Statistics/Statistics.swift b/Sources/Euler/Statistics/Statistics.swift index 109ba364..48e610da 100644 --- a/Sources/Euler/Statistics/Statistics.swift +++ b/Sources/Euler/Statistics/Statistics.swift @@ -33,3 +33,18 @@ public class Statistics: NSObject { self.list = list.map { BigNumber($0) } } } + +// MARK: Basic Methods for the list +public extension Statistics { + /// Returns the mode of the list. + /// + /// The mode is the value that appears most often in a set of data values. + var mode: BigNumber { + var counts: [BigNumber: Int] = [:] + for num in self.list { + counts[num, default: 0] += 1 + } + let sorted = counts.sorted { $0.value > $1.value } + return sorted[0].key + } +}