logo

Two to One

Take 2 strings s1 and s2 including only letters from a to z. Return a new sorted string, the longest possible, containing distinct letters - each taken only once - coming from s1 or s2.


My code:


                        function longest(s1, s2) {
                            let mixed = (s1 + s2).toLowerCase().split('').sort();
                            let sorted = [];
                            for (let i = 0; i < mixed.length; i++) {
                              if (sorted.indexOf(mixed[i]) === -1) {
                                sorted.push(mixed[i]);
                              }
                            }
                            return sorted.join('');
                          }                           
                    


And that's the best solution among all users:


                        function longest(s1, s2) {
                            return Array.from(new Set(s1 + s2)).sort().join('');
                          }
                    

New things I learned today: