function mode
mode(
x: array,
dim?: 0 | 1,
): number

Most frequent value in an array.

Finds the most frequently occurring value in an array or matrix. In case of ties, returns the first value that achieves the maximum frequency.

Examples

Simple mode

import { assertEquals } from "jsr:@std/assert";

assertEquals(mode([1, 2, 2, 3]), 2);

Mode with multiple values

import { assertEquals } from "jsr:@std/assert";

assertEquals(mode([1, 1, 2, 2, 2]), 2);

Matrix mode

import { assertEquals } from "jsr:@std/assert";

assertEquals(mode([[1, 1, 2], [3, 3, 4]]), [1, 3]);

Parameters

Input array or matrix

optional
dim: 0 | 1

Dimension along which to compute mode. Default is 0

Return Type

number

Most frequent values

Throws

When input is invalid

mode(
x: matrix,
dim?: 0 | 1,
): array

Most frequent value in an array.

Finds the most frequently occurring value in an array or matrix. In case of ties, returns the first value that achieves the maximum frequency.

Examples

Simple mode

import { assertEquals } from "jsr:@std/assert";

assertEquals(mode([1, 2, 2, 3]), 2);

Mode with multiple values

import { assertEquals } from "jsr:@std/assert";

assertEquals(mode([1, 1, 2, 2, 2]), 2);

Matrix mode

import { assertEquals } from "jsr:@std/assert";

assertEquals(mode([[1, 1, 2], [3, 3, 4]]), [1, 3]);

Parameters

Input array or matrix

optional
dim: 0 | 1

Dimension along which to compute mode. Default is 0

Return Type

Most frequent values

Throws

When input is invalid