diff --git a/PeyrSharp.Extensions/DoubleExtensions.cs b/PeyrSharp.Extensions/DoubleExtensions.cs
index 2bad1b7..7000e78 100644
--- a/PeyrSharp.Extensions/DoubleExtensions.cs
+++ b/PeyrSharp.Extensions/DoubleExtensions.cs
@@ -259,5 +259,25 @@ public static double Median(this double[] values)
return median;
}
+ ///
+ /// Calculates the mode of a dataset.
+ ///
+ /// The dataset to calculate the mode of.
+ /// The mode of the dataset.
+ /// Thrown if the dataset is empty.
+ public static double Mode(this double[] values)
+ {
+ // Check for empty input
+ if (values.Length == 0)
+ {
+ throw new ArgumentException("Cannot calculate mode of empty dataset", "values");
+ }
+
+ // Group values by frequency
+ var frequencyGroups = values.GroupBy(x => x).OrderByDescending(g => g.Count());
+
+ // Return most common value (if more than one, return first)
+ return frequencyGroups.First().Key;
+ }
}
}
diff --git a/PeyrSharp.Extensions/IntExtensions.cs b/PeyrSharp.Extensions/IntExtensions.cs
index 33ff573..fb5ee29 100644
--- a/PeyrSharp.Extensions/IntExtensions.cs
+++ b/PeyrSharp.Extensions/IntExtensions.cs
@@ -124,5 +124,25 @@ public static double Median(this int[] values)
return median;
}
+ ///
+ /// Calculates the mode of a dataset.
+ ///
+ /// The dataset to calculate the mode of.
+ /// The mode of the dataset.
+ /// Thrown if the dataset is empty.
+ public static double Mode(this int[] values)
+ {
+ // Check for empty input
+ if (values.Length == 0)
+ {
+ throw new ArgumentException("Cannot calculate mode of empty dataset", "values");
+ }
+
+ // Group values by frequency
+ var frequencyGroups = values.GroupBy(x => x).OrderByDescending(g => g.Count());
+
+ // Return most common value (if more than one, return first)
+ return frequencyGroups.First().Key;
+ }
}
}