Array

Arrays are one of the mostly used data structures in JavaScript. utilict supports arrays of numbers, strings, binary variables and objects. Following operations are supported on array data type.

Minimum Array

Returns the k’th minimum element in the array. k is the 1-indexed variable with the default value as 1.

Usage

minArray([1, 3, 2, 6, 5, 8, 7, 0, 3]); // Returns 0
minArray([32, 67, 98, 23, 54, 61, 29, 74], 4); // Returns 54

Arguments

  • list: Arary of numbers.
  • k (optional): The minimum index, default value is 1.

Returns

Returns k’th minimum element in the array.

Maximum Array

Returns the k’th maximum element in the array. k is the 1-indexed variable with the default value as 1.

Usage

maxArray([1, 3, 2, 6, 5, 8, 7, 0, 3]); // Returns 8
maxArray([32, 67, 98, 23, 54, 61, 29, 74], 4); // Returns 61

Arguments

  • list: Arary of numbers.
  • k (optional): The maximum index, default value is 1.

Returns

Returns k’th maximum element in the array.

Array Product

Returns the product of all the numbers in the array.

Usage

arrayProduct([2, 3, 6, 34, 90, 1]); // Returns 110160
arrayProduct([90, 87, 0, -34, -900, -1245]); // Returns 0

Returns

Returns the product of all the numbers in the array.

Mean

Returns the mean of the given array. utilict supports 4 types of mean, arithmetic, geometric, harmonic, rms. The default value is arithmetic.

Usage

mean([3, 5, 9, 5, 7, 2]); // Performs arithmetic mean, returns 5.17
mean([4, 10, 16, 24], "geometric"); // Returns 11.13
mean([10, 2, 4, 7], "harmonic"); // Returns 4.03
mean([2, 3, 5, 7, 11], "rms"); // Returns 6.45

Arguments

  • list: Arary of numbers.
  • type (optional): Mean type, supported types are arithmetic, geometric, harmonic, rms, Default type is arithmetic.

Returns

Returns the mean of the list.

Median

Returns the median of the given array. Median is the middle number in the sorted array. Sorting is performed internally by the utilict.

Usage

median([142, 140, 130, 150, 160, 135, 158, 132]); // Returns 141
median([4, 12, 14, 17, 22, 23, 23, 24, 25, 29, 40, 67, 77, 82, 92]); // Returns 24

Returns

Returns the median of the list.

Mode

Returns the mode of the given list. Mode is the value that appears most frequently within that list. If multiple numbers have the same highest frequency, it will return an array of mode values.

Usage

mode([3, 3, 6, 9, 16, 16, 16, 27, 27, 37, 48]); // Returns  16
mode([3, 3, 3, 9, 16, 16, 16, 27, 37, 48]); // Returns [3, 16]

Returns

Returns the mode of the list.

Root Mean Square

Calculates the RMS (Root Mean Square) of the list. RMS is the square root of the mean square.

Usage

rootMeanSquare([1, 2, 3, 4, 5]); // Returns 3.32
rootMeanSquare([4, 6, 8, 10]); // Returns 7.35

Returns

Returns the Root Mean Square (RMS) of the list.

Variance

Variance is a measure of the spread of data within the list.

Usage

variance([46, 69, 32, 60, 52, 41]); // Returns 177.2
variance([4, 2, 5, 8, 6]); // Returns 5

Returns

Returns the variance of the list.

Standard Deviation

Standard Deviation is the square root of variance.

Usage

standardDeviation([46, 69, 32, 60, 52, 41]); // Returns 13.31
standardDeviation([4, 2, 5, 8, 6]); // Returns 2.24

Returns

Returns the standard deviation of the list.

Remove Duplicates

Removes duplicates from the list and return the same list.

Usage

removeDuplicates([1, 2, 3, 3, 2, 1, 4, 5, 1, 5, 8, 3]); // Returns [1, 2, 3, 4, 5, 8]
removeDuplicates([52, 56, 41, 98, 52, 41, 109, 254]); // Returns [52, 56, 41, 98, 109, 254]

Returns

Same list with duplicates removed

Histogram

Returns the histogram map for each item occurrence in a given list.

Usage

histogram([3, 3, 3, 9, 16, 16, 16, 27, 37, 48]); // Returns {"3": 3, "9": 1, "16": 3, "27": 1, "37": 1, "48": 1}

Returns

Histogram map for a given list.

Greatest Common Divisor (GCD)

Returns the Greatest Common Divisor (GCD) of the list.

Usage

gcd([8, 12, 16]); // Returns 4
gcd([13, 17, 48, 91]); // Returns 1
gcd([]); // Returns 0

Returns

Returns Greatest Common Divisor (GCD) of the list.

Least Common Multiple (LCM)

Returns the Least Common Multiple (LCM) of the list.

Usage

lcm([12, 18, 24]); // Returns 72
lcm([10, 12, 15, 75]); // Returns 300
lcm([]); // Returns 0

Returns

Returns Least Common Multiple (LCM) of the list.