UNPKG

everyutil

Version:

A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.

30 lines (29 loc) 735 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compressArray = void 0; /** * Compresses array using run-length encoding. * @author @dailker * @template T * @param {T[]} array * @returns {[T, number][]} */ function compressArray(array) { if (!array.length) return []; const result = []; let prev = array[0], count = 1; for (let i = 1; i < array.length; i++) { if (array[i] === prev) { count++; } else { result.push([prev, count]); prev = array[i]; count = 1; } } result.push([prev, count]); return result; } exports.compressArray = compressArray;