Skip to content

Commit

Permalink
Added mode to statistics object
Browse files Browse the repository at this point in the history
  • Loading branch information
arguiot committed Sep 8, 2022
1 parent 1456454 commit d00bca4
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Sources/Euler/Statistics/Statistics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit d00bca4

Please sign in to comment.