Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
Merging two strings that are the same, that appear in a row within an array using JS

For example:

const items = ['a', 'a', 'b', 'a', 'a'];

Would become:

['a', 'b', 'a']

Is there a clean way of doing the above using JS?

I have tried:

const items = ['a', 'a', 'b', 'a', 'a'];
const uniqueInRow = [];

for (var i=0; i < items.length; i++) {
  item = items[i];

  if (item !== item[i++]) {
    uniqueInRow.push(item);
  }
}

console.log(uniqueInRow);

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try like following:

const items = ['a', 'a', 'b', 'a', 'a'];
const res = []
for(let i = 0; i < items.length; i++) {
  if (i === 0 || items[i] !== items[i-1]) res.push(items[i])
}

console.log(res)

about 4 years ago · Juan Pablo Isaza Report

0

You may avoid equal neighbours via oneliner:

vals.forEach((v, i) => (i < vals.length && v === vals[i + 1]) && vals.splice(i, 1))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!