RealDocs

UKismetMathLibrary::MedianOfIntArray

function Engine Blueprint Since 4.20
#include "Kismet/KismetMathLibrary.h"
Access: public Specifiers: staticBlueprintPureUFUNCTION

Description

Computes the median of an integer array, returning the middle element for odd-length arrays or the average of the two middle elements for even-length arrays. Returns 0 for empty arrays.

Caveats & Gotchas

  • The array parameter is taken by value (a copy is made and sorted internally) — passing a large array causes a heap allocation and sort on each call, so avoid calling this in a tight loop.
  • The output type is float, not int32 — even-length arrays produce a fractional median (e.g. median of {1,2} is 1.5).

Signature

static ENGINE_API void MedianOfIntArray(TArray<int32> IntArray, float& MedianValue);

Parameters

Name Type Description Default
IntArray TArray<int32> The array to find the median of. Passed by value — the function sorts a copy internally.
MedianValue float& Output: the median value, averaged for even-length arrays.

Return Type

void

Example

Median of a score array C++
TArray<int32> Scores = { 10, 30, 20, 50, 40 };
float Median;
UKismetMathLibrary::MedianOfIntArray(Scores, Median);
// Median == 30.0f

Version History

Introduced in: 4.20

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.