List (Array)
Mean (Average Calculation)
You can calculate the mean of a list. Supported mean types are arithmetic
, geometric
, harmonic
and rms (root mean square)
. By default arithmetic
is selected.
import { mean } from "quark-dsa";
let list = [2, 6, 4, 5, 8];
mean(list); // returns 5
mean(list, "geometric"); // returns 4.54
mean(list, "harmonic"); // returns 4.03
mean(list, "rms"); // returns 5.39
Median
You can calculate middle value of the list, which is median. If list has even number of items, it will return average of middle two values, otherwise it will return middle value. List will get sorted in ascending order before median calculation.
import { median } from "quark-dsa";
let list = [4, 12, 14, 17, 22, 23, 23, 24, 25, 29, 40, 67, 77, 82, 92];
median(list); // returns 24
let list2 = [142, 140, 130, 150, 160, 135, 158, 132];
median(list2); // returns 141
Mode
You can also get the element having highest frequency in the list, which is mode. If the list has only one mode, then it will return as a number. If the list has more than one modes, then it will return all mode values in an array.
import { mode } from "quark-dsa";
let list = [3, 3, 6, 9, 16, 16, 16, 27, 27, 37, 48];
mode(list); // returns 16
let list2 = [3, 3, 3, 9, 16, 16, 16, 27, 37, 48];
mode(list2); // returns [3, 16]
Remove duplicates
You can remove duplicates from a list and returns the same list. Here, in place operation is performed on the list. Only numbers and strings are supported in the list.
import { removeDuplicates } from "quark-dsa";
let list = [1, 2, 3, 3, 2, 1, 4, 5, 1, 5, 8, 3];
removeDuplicates(list); // returns [1, 2, 3, 4, 5, 8];
let list2 = [
"John",
"George",
"Paul",
"Ringo",
"George",
"Bob",
"Elvis",
"Freddie",
"Paul",
];
removeDuplicates(list2); // returns ["John", "George", "Paul", "Ringo", "Bob","Elvis", "Freddie"]