0

I have seen a lot of solutions for this but most of them implement the sorting function like arr.sort() which can be used with letters and integers.

Take a look at my array

arr = ["COINS", "SWORDS",  "COINS", "COINS", "STARS", "SWORDS", "KNIGHTS"];

What I want is to count all occurrences of the duplicate strings. For example, based on my array. I want something like this as a result

arr = ["COINS", "SWORDS",  "COINS", "COINS", "STARS", "SWORDS", "KNIGHTS"];
result => 5 (3 coins + 2 swords)

Anyone can tell me how to solve this?

1 Answers1

0

The most logical way is to sort the array, then count the duplicates.

See here for an example: Counting array elements and sort descending by count

Community
  • 1
  • 1
jsdeveloper
  • 3,945
  • 1
  • 15
  • 14