I had an interview for a company that gave me the following exercise as part of the technical test.
Given the array let array = ['ab', 'abb', 'abbabbabbaa']
remove the repeated contiguous letters and return the array sorted from highest to lowest.
The expected output was :['abababa','ab','ab']
But I couldn't solve it, I knew I should use map
and filter
but I didn't know how to fix it. My solution was to iterate each of the words with map and convert to a set each one to eliminate the repeated ones but it eliminated all the repeated letters of each position leaving something like the following ['ab','ab','ab']
.
my solution was:
let data = ["ab", "abb", "abbabbabbaa"]
const newData = data.map((el, index) => [...new Set(el)])
console.log(newData)
How would you solve it? I wouldn't want to find myself back with the same problem without a solution! Thanks in advance
One solution is to loop through each string from the second element (if it has more than one element), and compare the iterand to the previous element. In the end it's about doing it just like you would on paper and pencil.
For example:
We first store the first character (
cadena[0]
) in another string variable calledsalida
.If
cadena
it has more than one element, we iterate fromi
equal to 1 tocadena.length - 1
.At each iteration we compare
cadena[i]
withcadena[i - 1]
.If they are equal, we continue to the next iteration.
If they are different, we concatenate the character
cadena[i]
withsalida
.Finally we return output.
In code it looks like this:
I hope this helps you solve the problem.
Let's first make clear how the
Set
. According to the documentation:The above means that it will
Set
only store unique values , that is, if you create a being from[1, 1, 2, 2, 3, 4, 4]
, the output will be[1, 2, 3, 4]
. That's why in your code, the output is alwaysab
.Returning to the problem, you are not so bad with the idea of using
.map()
, only that you lacked developing the logic a little more, it is understandable, it has happened to me in interviews. What you could do, for example, would be to go through each letter of the string as if it were an array, create an auxiliary array in which you insert the element, if and only if, it is different from the last element in the array, that is, if the last element isa
and the one you evaluate to isa
, then you don't insert it. Conversely, if the last element isa
and you are evaluatingb
, then it is not an equal element and you can insert it into the new array.In the example it is assumed that each word has a length greater than 2, but ideally you should validate that to avoid problems with the for (I am not validating). You start from the second element (index
1
) and evaluate with respect to the previous one until traversing each element. At the end, you make a.join()
of that array to get it as a string and not as an array.You could achieve something similar by using
.reduce()
y.slice()
(to get the last character). This way you forget about validating the length of the string and you can also work directly with a string.