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

145
Views
How can I organize an array based on a pattern?

I'm pretty new to coding and I'm practicing some array manipulation. Basically, I'm trying to organize this array:

const sequence = ["abc", "abcd", "bca", "bdc", "cbd", "bdc", "acb"];

to something like this:

[[ 'abc', 'bca', 'acb'], ['bdc', 'cbd', 'bdc'], ['abcd']]

So far, this is what I have:

const sequence = ["abc", "abcd", "bca", "bdc", "cbd", "bdc", "acb"];

let finalSequence = [];
let sequence2 = [];
let sequence3 = [];
let sequence4 = [];
sequence.forEach(element => {
  for (const elem of element ) {
    sequence2 = sequence.filter(e => e.includes(elem) && e.length === element.length)
    sequence3 = sequence.filter(e => !e.includes(elem))
    sequence4 = sequence.filter(e => e.includes(elem) && e.length !== element.length)
    return sequence2, sequence3, sequence4
  }
})
console.log(sequence2)
console.log(sequence3)
console.log(sequence4)

finalSequence.push(sequence2, sequence3, sequence4)

This only works for the array provided but once I start testing it and playing around with it a bit more, it becomes evident this is not the proper way to go about it.

Would really appreciate any suggestions or input on how to tackle this problem. Thank you so much!

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

0

You could normalize each string to get sorted letters and use this as group for all similar strings.

const
    sequence = ["abc", "abcd", "bca", "bdc", "cbd", "bdc", "acb"],
    grouped = Object.values(sequence.reduce((r, s) => {
        const key = Array.from(s).sort().join('');
        (r[key] ??= []).push(s);
        return r;
    }, {}));

console.log(grouped);

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!