Codewars - Two to One
Nicolás Bost

Nicolás Bost @dantedinopegassi

About: Yo tratando de usar un 𝒷𝓁ℴ𝑔 para jugar menos jueguitos

Joined:
Dec 30, 2024

Codewars - Two to One

Publish Date: Jan 5
0 1

Salutations.

Dave says hi

I'm posting Codewars challenges and my thought process in this series. I'm using JS and Node 18 whenever possible. Just for the sake of clarity, I'm making fair use of them.

Ok, this was juicy. In essence, you have to remove duplicates in a string, order what's left and join two strings. Not exactly in that order. Actually, I did it in the exact opposite way. And I also built an utility function for a very specific step (removing duplicates).


First, concatenate two strings. Easy:

let longest = s1 + s2;
Enter fullscreen mode Exit fullscreen mode

Second, split up the letters:

let array = new Array(s1.length + s2.length);
for (let i = 0 ; i < s1.length + s2.length ; i++ ){
    array[i] = longest[i];
  }
Enter fullscreen mode Exit fullscreen mode

Third, sort the letters:

array = array.sort((a, b) => a.localeCompare(b));
// (a, b) => a.localeCompare(b)  THIS IS REDUNDANT, but I leave it as a reminder
Enter fullscreen mode Exit fullscreen mode

Fourth, remove duplicates:

function keepFirstLetter(array){
  let arrayFixed = new Array(array.length);
  let counter = 0;
  arrayFixed[0] = array[0];


  for (let i = 0 ; i < array.length ; i++ ){
    if (arrayFixed[counter] != array[i]){
      counter++;
      arrayFixed[counter] = array[i];
    }
  }

  return arrayFixed;
}
Enter fullscreen mode Exit fullscreen mode

Fifth, clean up:

array = array.join("");
Enter fullscreen mode Exit fullscreen mode

Last, place every piece of the puzzle together:

function longest(s1, s2) {
  let array = new Array(s1.length + s2.length);

  let longest = s1 + s2;

  for (let i = 0 ; i < s1.length + s2.length ; i++ ){
    array[i] = longest[i];
  }

  array = array.sort((a, b) => a.localeCompare(b));

  array = keepFirstLetter(array);

  array = array.join("")

  return array;
}

function keepFirstLetter(array){
  let arrayFixed = new Array(array.length);
  let counter = 0;
  arrayFixed[0] = array[0];

  for (let i = 0 ; i < array.length ; i++ ){
    if (arrayFixed[counter] != array[i]){
      counter++;
      arrayFixed[counter] = array[i];
    }
  }

  return arrayFixed;
}
Enter fullscreen mode Exit fullscreen mode

It's definitely NOT performant. I don't like that second loop. And it's not so clean either. Someday I'll need to revisit this one.

Be well. Drink water 💧💧💧.

Previous

Comments 1 total

  • Alex Mustiere
    Alex MustiereJan 6, 2025

    I propose a more simple approach for this "problem":

    const towToOne = (s1, s2) => {
      // concatenate and transform to array
      const concat = (s1 + s2).split('');
    
      // remove duplicates
      const cleanConcat = [...new Set(concat)];
    
      // sort
      const sortedConcat = cleanConcat.sort((a, b) => a.localeCompare(b));
    
      // return as string
      return sortedConcat.join('');
    };
    
    const [s1, s2] = ['dbacfe', 'cdgdchid'];
    const result = towToOne(s1, s2);
    console.log(`towToOne('${s1}', '${s2}') is '${result}'`);
    
    Enter fullscreen mode Exit fullscreen mode

    You can simplify the twoToOne function if you want:

    const towToOne = (s1, s2) => {
      return [...new Set((s1 + s2).split(''))].sort((a, b) => a.localeCompare(b)).join('');
    };
    
    Enter fullscreen mode Exit fullscreen mode
Add comment