Open In App

Collect.js duplicates() Method

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:



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:




// 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.




// Importing the collect.js module.
const collect = require('collect.js'); 
    
const collection = collect(["geeks"
    "geeks", "for", "geeks", "for"]);
  
const duplicatesObject = collection.duplicates();
  
// Logging the return type
// logging the duplicates object using
// duplicate.all
console.log("Output: ")
console.log(duplicatesObject.all())

Output:


Article Tags :