@pelevesque/remove-duplicates
v1.0.1
Published
Removes and returns duplicates from an array.
Downloads
59
Readme
remove-duplicates
Removes and returns duplicates from an array.
Node Repository
https://www.npmjs.com/package/@pelevesque/remove-duplicates
Installation
npm install @pelevesque/remove-duplicates
Tests
Standard Style & Unit Tests
npm test
Unit Tests & Coverage
npm run cover
Usage
Requiring
const removeDuplicates = require('@pelevesque/remove-duplicates')
Basic
const arr = [
'd131dd02c5e6eec4', // a
'55ad340609f4b302',
'd8823e3156348f5b', // b
'd131dd02c5e6eec4', // a
'd8823e3156348f5b', // b
'551111111114b302', // c
'551111111114b302' // c
]
const duplicates = removeDuplicates(arr)
/*
arr = [
'd131dd02c5e6eec4',
'55ad340609f4b302',
'd8823e3156348f5b',
'551111111114b302'
]
duplicates = [
'd131dd02c5e6eec4',
'd8823e3156348f5b',
'551111111114b302'
]
*/
OnlyKeepElementsWithoutDuplicates Option
When setting the onlyKeepElementsWithoutDuplicates
to true, only elements that
have no duplicates are kept.
const arr = [
'd131dd02c5e6eec4', // a
'55ad340609f4b302',
'd8823e3156348f5b', // b
'd131dd02c5e6eec4', // a
'd8823e3156348f5b', // b
'551111111114b302', // c
'551111111114b302' // c
]
const opts = { onlyKeepElementsWithoutDuplicates: true }
const duplicates = removeDuplicates(arr, opts)
/*
arr = [
'55ad340609f4b302'
]
duplicates = [
'd131dd02c5e6eec4',
'd8823e3156348f5b',
'd131dd02c5e6eec4',
'd8823e3156348f5b',
'551111111114b302',
'551111111114b302'
]
*/
Remove option
With the remove option set to false, duplicates are still returned, but nothing is removed from the original array.
const arr = [
'd131dd02c5e6eec4', // a
'55ad340609f4b302',
'd8823e3156348f5b', // b
'd131dd02c5e6eec4', // a
'd8823e3156348f5b', // b
'551111111114b302', // c
'551111111114b302' // c
]
const opts = { remove: false }
const duplicates = removeDuplicates(arr, opts)
/*
arr = [
'd131dd02c5e6eec4',
'55ad340609f4b302',
'd8823e3156348f5b',
'd131dd02c5e6eec4',
'd8823e3156348f5b',
'551111111114b302',
'551111111114b302'
]
duplicates = [
'd131dd02c5e6eec4',
'd8823e3156348f5b',
'551111111114b302'
]
*/