@pelevesque/find-duplicates
v0.1.1
Published
Finds indices of duplicates in an array of elements.
Downloads
4
Readme
find-duplicates
Finds indices of duplicates in an array of elements.
find-duplicates uses lodash.isequal to make comparisons, so any type supported by lodash.isequal is supported by find-duplicates.
Node Repository
https://www.npmjs.com/package/@pelevesque/find-duplicates
Installation
npm install @pelevesque/find-duplicates
Tests
Standard Style & Unit Tests
npm test
Unit Tests & Coverage
npm run cover
Usage
Requiring the Module
const findDuplicates = require('@pelevesque/find-duplicates')
One Set of Duplicates
const elements = [
'd131dd02c5e6eec4', // a
'55ad340609f4b302',
'd131dd02c5e6eec4', // a
'd131dd02c5e6eec4', // a
'd8823e3156348f5b'
]
const result = findDuplicates(elements)
// result === [[0, 2, 3]]
Many Sets of Duplicates
const elements = [
'd131dd02c5e6eec4', // a
'55ad340609f4b302',
'd8823e3156348f5b', // b
'd131dd02c5e6eec4', // a
'd8823e3156348f5b', // b
'551111111114b302', // c
'551111111114b302' // c
]
const result = findDuplicates(elements)
// result === [[0, 3], [2, 4], [5, 6]]
Using the stopAtFirstSet Flag
// When using the stopAtFirstSet flag, only the first set is returned.
// Note: In this case, an array is returned instead of an array of arrays.
const elements = [
'd131dd02c5e6eec4', // a
'55ad340609f4b302',
'd8823e3156348f5b', // b
'd131dd02c5e6eec4', // a
'd8823e3156348f5b', // b
'551111111114b302', // c
'551111111114b302' // c
]
const stopAtFirstSet = true
const result = findDuplicates(elements, stopAtFirstSet)
// result === [0, 3]