Skip to content

Commit

Permalink
Add more Int Array extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim-Maes committed Dec 13, 2023
1 parent 96b6198 commit 6d2aa89
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,20 @@ Or just copy whatever you need from this repository :)
|-----------------------------------|---------------------------------------------------|
|`AverageIgnoringZero`| Calculates the average of the elements in the array, ignoring zero values.|
|`FindPrimeNumbers`| Filters the array to include only prime numbers.|
|`GCD`| Calculates the Greatest Common Divisor of all array elements.|
|`IsMonotonicallyIncreasing`| Checks if the array's elements are in a strictly increasing order. |
|`IsMonotonicallyDecreasing`| Checks if the array's elements are in a strictly decreasing order.|
|`LCM`| Finds the Least Common Multiple of all array elements.|
|`MultiplyAll`| Multiplies all elements in the array and returns the product.|
|`Mode`| Finds the most frequently occurring number(s) in the array.|
|`Percentile`| Finds the percentile value in the array.|
|`Randomize`| Randomizes the order of elements in the array.|
|`StandardDeviation`|Calculates the standard deviation of the array elements.|
|`SumAbsoluteDifferences`| Calculates the sum of the absolute differences between all pairs of elements.|
|`SumEvenNumbers`| Sums all the even numbers in the array.|
|`SumOddNumbers`| Sums all the odd numbers in the array.|
|`SumOddNumbers`| Sums all the odd numbers in the array.|
|`ToFrequencyMap`| Creates a frequency map (count of each element).|
|`Variance`| Calculates the variance of the array elements.|

## License

Expand Down
101 changes: 101 additions & 0 deletions src/ArrayExtensions/IntArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,105 @@ private static bool IsPrime(int number)
}
return true;
}

/// <summary>
/// Checks if the array's elements are in a strictly increasing order.
/// </summary>
/// <param name="arr">The array to check.</param>
/// <returns>True if the array is strictly increasing; otherwise, false.</returns>
public static bool IsMonotonicallyIncreasing(this int[] arr)
=> arr.Zip(arr.Skip(1), (a, b) => a < b).All(x => x);

/// <summary>
/// Checks if the array's elements are in a strictly decreasing order.
/// </summary>
/// <param name="arr">The array to check.</param>
/// <returns>True if the array is strictly decreasing; otherwise, false.</returns>
public static bool IsMonotonicallyDecreasing(this int[] arr)
=> arr.Zip(arr.Skip(1), (a, b) => a > b).All(x => x);

/// <summary>
/// Finds the most frequently occurring number(s) in the array.
/// </summary>
/// <param name="arr">The array to process.</param>
/// <returns>An enumerable of the most frequent number(s).</returns>
public static IEnumerable<int> Mode(this int[] arr)
=> arr.GroupBy(n => n).OrderByDescending(g => g.Count()).Select(g => g.Key);

/// <summary>
/// Calculates the Greatest Common Divisor of all array elements.
/// </summary>
/// <param name="arr">The array to process.</param>
/// <returns>The greatest common divisor of the array elements.</returns>
public static int GCD(this int[] arr)
=> arr.Aggregate((x, y) => GCD(x, y));
private static int GCD(int a, int b)
=> b == 0 ? a : GCD(b, a % b);

/// <summary>
/// Finds the Least Common Multiple of all array elements.
/// </summary>
/// <param name="arr">The array to process.</param>
/// <returns>The least common multiple of the array elements.</returns>
public static long LCM(this int[] arr)
=> arr.Aggregate(1L, (a, b) => a * b / GCD(a, b));

/// <summary>
/// Finds the percentile value in the array.
/// </summary>
/// <param name="arr">The array to process.</param>
/// <param name="percentile">The percentile to find (0-100).</param>
/// <returns>The value at the specified percentile.</returns>
public static double Percentile(this int[] arr, double percentile)
{
Array.Sort(arr);
int index = (int)Math.Ceiling(percentile / 100.0 * arr.Length) - 1;
return arr[index];
}

/// <summary>
/// Randomizes the order of elements in the array.
/// </summary>
/// <param name="arr">The array to randomize.</param>
/// <returns>A new array with elements in random order.</returns>
public static int[] Randomize(this int[] arr)
{
var rnd = new Random();
return arr.OrderBy(x => rnd.Next()).ToArray();
}

/// <summary>
/// Calculates the sum of the absolute differences between all pairs of elements.
/// </summary>
/// <param name="arr">The array to process.</param>
/// <returns>The sum of absolute differences between all pairs.</returns>
public static long SumAbsoluteDifferences(this int[] arr)
=> arr.SelectMany((x, i) => arr.Skip(i + 1), (x, y) => Math.Abs(x - y)).Sum();

/// <summary>
/// Creates a frequency map (count of each element).
/// </summary>
/// <param name="arr">The array to process.</param>
/// <returns>A dictionary with the frequency of each element.</returns>
public static Dictionary<int, int> ToFrequencyMap(this int[] arr)
=> arr.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count());

/// <summary>
/// Calculates the variance of the array elements.
/// </summary>
/// <param name="arr">The array to process.</param>
/// <returns>The variance of the array elements.</returns>
public static double Variance(this int[] arr)
{
double mean = arr.Average();
return arr.Sum(num => Math.Pow(num - mean, 2)) / arr.Length;
}

/// <summary>
/// Calculates the standard deviation of the array elements.
/// </summary>
/// <param name="arr">The array to process.</param>
/// <returns>The standard deviation of the array elements.</returns>
public static double StandardDeviation(this int[] arr)
=> Math.Sqrt(arr.Variance());
}

0 comments on commit 6d2aa89

Please sign in to comment.