# Counting Consecutive Elements of an Array in JavaScript

I failed the interview question. Let’s make sure we both learn a lesson.

I recently had a tech interview where I was asked this question:

Given an array of characters return a string that counts the number of each consecutive letter.

Example: [“a”, “b”, “b”, “a”] should return “a1b2a1”

Seems simple enough. I’m sure a nested for loop…

Maybe it was nerves. But I simply couldn’t figure it out in the allotted 20 minutes. After licking my wounds, I got back up on that horse, and looked at it with fresh eyes with the help of a friend. It now seemed so simple.

I think my major flaw in the interview is that I didn’t follow my own advice: do it as a human first. Take notes of what I did. Pseudocode that. Then translate to code. Let’s do that here (practice makes perfect, right?):

1. I see that my array’s first element is “a”. So I take note of that.
2. I look at the next element. It’s a “b”, which is not like my first. So, I should just make a mental note that my string should start with “a1”.
3. Now I’m looking at a “b”, and the following one is also a “b”, but the one after that is not a “b”, so I should make a mental note of “b2”.
4. Last one is “a”, so add “a1” to my mental note.

This is how I naturally do it. And I can immediately tell that,

• I’m iterating
• I’m looking ahead of my current iteration
• I’m accumulating the number of times I’m seeing an element
• I’m adding it to a my mental note based on certain conditions

Let’s start translating this to code. First I’m going to initialize a couple of variables:

• A result string to house my answer
• A counter variable to count the number of times I’m seeing an element. I initialize the counter with 1.
`function cntConsecutiveElements(array) {  let result = "";  let counter = 1;}`

Next we iterate. A simple for loop will suffice. Let’s try with the example: [“a”, “b”, “b”, “a”]

• The first element I see will start with a counter of 1. If the following element is the same as the one I’m currently iterating with, I should increment my counter:
`function cntConsecutiveElements(array) {  let result = "";  let counter = 1;for (let i = 0; i < array.length; i++) {    if (array[i] === array[i + 1]) {      counter++;    }   }  return result;}`
• If it’s not, then I should add the element, and the counter together, and then add that to my result. After iterating is complete, I should return the result:
`(1) function cntConsecutiveElements(array) {(2)   let result = "";(3)   let counter = 1;(4) for (let i = 0; i < array.length; i++) {(5)    if (array[i] === array[i + 1]) {(6)      counter++;(7)    } else {(8)      result += array[i] + counter;(9)      counter = 1;(10)    }(11)  }(12)   return result;(13) }`

Let’s walk through the code with our example:

• When i = 0, array is “a”
• At line 5 we ask: is “a” = array? Well, array is “b”, so the answer is no.
• We hit the else statement in line 7 and concatenate “a” and 1 to form “a1”. We then add that to our result, which is now “a1”.
• Back to the top of the loop. When i = 1, array is “b”.
• Again we ask: is “b” = array? This time it is, so we increment our counter to 2.
• When i = 2, array is “b” again.
• Again we ask: is “b” = array. Array is “a”, so the answer is no.
• We hit the else statement and concatenate “b” and 2 to form “b2”. We then add that to our result, which is now “a1b2”.
• When i = 3, array is “a”.
• Again we ask: is “a” = array? Well, array is undefined. So definitely not. We hit the else statement and concatenate “a” and 1 to form “a1”. We then add that to our result, which is now “a1b2a1”.
• Iteration is now completed, and we return the string “a1b2a1”.

It was a bit disheartening to receive this relatively simple interview question, and not be able to knock it out of the park. But this is why I always say…

Grit > talent

I’ll get it next time :-)