Skip to content
Advertisement

Count duplicate lists inside a list

JavaScript

I want the result to be 2 since number of duplicate lists are 2 in total. How do I do that?

I have done something like this

JavaScript

But the count value is 1 as it returns matched lists. How do I get the total number of duplicate lists?

Advertisement

Answer

Solution

You can use collections.Counter if your sub-lists only contain numbers and therefore are hashable:

JavaScript

In Steps

Convert your sub-list into tuples:

JavaScript

Count them:

JavaScript

take only the values:

JavaScript

Finally, sum only the ones that have a count greater than 1:

JavaScript

Make it Re-usable

Put it into a function, add a docstring, and a doc test:

JavaScript

Run the test:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement