OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I count items in a JavaScript array but only when items are the same next to each other?

  • Thread starter Thread starter Patrik Michnac
  • Start date Start date
P

Patrik Michnac

Guest
I have an array for example like this:

{John, John, John, Maria, Peter, Peter, Maria, Anna, Anna, Maria, Maria, Peter}

I need to get result like:

1 -> 3
2 -> 1
3 -> 2
4 -> 1
5 -> 2
6 -> 2
7 -> 1

<p>I have an array for example like this:</p>
<p>{John, John, John, Maria, Peter, Peter, Maria, Anna, Anna, Maria, Maria, Peter}</p>
<p>I need to get result like:</p>
<p>1 -> 3<br />
2 -> 1<br />
3 -> 2<br />
4 -> 1<br />
5 -> 2<br />
6 -> 2<br />
7 -> 1</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top