@bemoje/arr-sorted-remove-duplicates

Remove all duplicate elements in a sorted array, leaving only uniques.

Usage no npm install needed!

<script type="module">
  import bemojeArrSortedRemoveDuplicates from 'https://cdn.skypack.dev/@bemoje/arr-sorted-remove-duplicates';
</script>

README

@bemoje/arr-sorted-remove-duplicates

Remove all duplicate elements in a sorted array, leaving only uniques.

Version

NPM version

Travis CI

dependencies

Dependencies

dependencies

Stats

NPM downloads Forks

Donate

Buy Me A Beer donate button PayPal donate button

Installation

npm install @bemoje/arr-sorted-remove-duplicates
npm install --save @bemoje/arr-sorted-remove-duplicates
npm install --save-dev @bemoje/arr-sorted-remove-duplicates

Usage


import arrSortedRemoveDuplicates from '@bemoje/arr-sorted-remove-duplicates'

const arr = [0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 7]

arrSortedRemoveDuplicates(arr, {
  numeric: true,
})
//=> [0, 1, 2, 3, 4, 5, 6, 7]

Tests

Uses Jest to test module functionality. Run tests to get coverage details.

npm run test

API

Table of Contents

arrSortedRemoveDuplicates

Remove all duplicate elements in a sorted array, leaving only uniques.

Parameters
  • arr Array The sorted array

  • compare (comparator | object)?

    • compare.numeric boolean Sort numerically. Defaults to lexicographic/alphabetic sort. (optional, default false)

    • compare.descending boolean Sort in descending order. Defaults to ascending order. (optional, default false)

    • compare.array boolean Sort arrays. Nested arrays are also compared recursively. (optional, default false)

    • compare.by (number | string | getter) Sort by either array index, a callback(element): any - or by object keys with dot-notation support. (optional, default undefined)

Returns arr

comparator

Comparator function callback definition.

Type: Function

Parameters
  • a any The first value to compare

  • b any The second value to compare

Returns number A negative number if a > b, a positive number if a < b, 0 otherwise.

getter

Value-getter function callback definition.

Type: Function

Parameters
  • a any The value

Returns any The value to be compared