Collect.js duplicates() Method
Last Updated :
29 Jul, 2020
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
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
const collect = require( 'collect.js' );
const collection = collect([ 'b' , 'c' , 'a' , 'a' , 'b' , "c" ]);
const duplicatesObject = collection.duplicates();
console.log( "Type is: " , typeof (duplicatesObject))
console.log(duplicatesObject)
|
Output:
Example 2: Logging using duplicate.all and extracting duplicates from an array of string.
Javascript
const collect = require( 'collect.js' );
const collection = collect([ "geeks" ,
"geeks" , "for" , "geeks" , "for" ]);
const duplicatesObject = collection.duplicates();
console.log( "Output: " )
console.log(duplicatesObject.all())
|
Output:
Share your thoughts in the comments
Please Login to comment...