Open In App

Collect.js duplicates() Method

Last Updated : 29 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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(["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:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads