The duplicates() function in collect.js is used to extract the duplicates from the given array. It returns an object containing key as the index of the duplicate and value as the duplicate itself.
Installation:
- Using NPM:
npm install collect.js
- Using CDN of collect.js
<script src="https://cdnjs.com/libraries/collect.js"></script>
Syntax:
collect.duplicates()
Parameters: This method does not accept any parameters.
Return Value: It returns the object containing duplicate values and their indices.
Example 1:
Javascript
// Importing the collect.js module. const collect = require( 'collect.js' ); const collection = collect([ 'b' , 'c' , 'a' , 'a' , 'b' , "c" ]); const duplicatesObject = collection.duplicates(); // Logging the return type console.log( "Type is: " , typeof (duplicatesObject)) // Logging the duplicates object console.log(duplicatesObject) |
Output:
Example 2: Logging using duplicate.all and extracting duplicates from an array of string.
Javascript
// Importing the collect.js module. const collect = require( 'collect.js' ); const collection = collect([ "neveropen" , "neveropen" , "for" , "neveropen" , "for" ]); const duplicatesObject = collection.duplicates(); // Logging the return type // logging the duplicates object using // duplicate.all console.log( "Output: " ) console.log(duplicatesObject.all()) |
Output: