समस्या
हमें एक JavaScript फ़ंक्शन लिखना है जो a से z तक के अक्षरों वाली दो स्ट्रिंग्स s1 और s2 लेता है। हमारा फ़ंक्शन एक नई सॉर्टेड स्ट्रिंग लौटानी चाहिए, जो सबसे लंबी संभव हो, जिसमें विशिष्ट अक्षर हों - प्रत्येक केवल एक बार लिया गया हो - जो s1 या s2 से आते हों।
उदाहरण
निम्नलिखिक कोड है −
const str1 = "xyaabbbccccdefww"; const str2 = "xxxxyyyyabklmopq"; const longestPossible = (str1 = '', str2 = '') => { const combined = str1.concat(str2); const lower = combined.toLowerCase(); const split =lower.split(''); const sorted = split.sort(); const res = []; for(const el of sorted){ if(!res.includes(el)){ res.push(el) } } return (res.join('')); }; console.log(longestPossible(str1, str2));आउटपुट
कंसोल आउटपुट निम्नलिखिक है −
abcdefklmopqwxy