-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
two-to-one.js
79 lines (66 loc) · 2.32 KB
/
two-to-one.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function longest(s1, s2) {
// get all unique letters from both s1 s2
const uniqueLetters = {};
// a place to store unique letters {}
// iterate over s1: for each letter
for (let i = 0; i < s1.length; i++) {
// if we have not see this letter, add it to the unique letters
const currentLetter = s1[i];
uniqueLetters[currentLetter] = true;
}
for (let i = 0; i < s2.length; i++) {
const currentLetter = s2[i];
uniqueLetters[currentLetter] = true;
}
// sort the unique letters
const letters = Object.keys(uniqueLetters);
// get all of the keys from the unique letters
// use built in sort to sort the keys
// return the unique letters sorted
return letters.sort().join('');
}
function longest(s1, s2) {
const uniqueLetters = {};
const addUniqueLetters = (str) => {
for (let i = 0; i < str.length; i++) {
const currentLetter = str[i];
uniqueLetters[currentLetter] = true;
}
};
addUniqueLetters(s1);
addUniqueLetters(s2);
const letters = Object.keys(uniqueLetters);
return letters.sort().join('');
}
function longest(s1, s2) {
const allLetters = s1 + s2;
const uniqueLetters = allLetters.split('').reduce((uniqueLetters, currentLetter) => {
uniqueLetters[currentLetter] = true;
return uniqueLetters;
}, {});
const letters = Object.keys(uniqueLetters);
return letters.sort().join('');
}
function longest(s1, s2) {
return Object.keys((s1 + s2).split('').reduce((uniqueLetters, currentLetter) => {
uniqueLetters[currentLetter] = true;
return uniqueLetters;
}, {})).sort().join('');
}
function longest(s1, s2) {
return Object.keys((s1 + s2).split('').reduce((all, l) => { all[l] = true; return all; }, {})).sort().join('');
}
function longest(s1, s2) {
return Object.keys((s1 + s2).split('').reduce((all, l) => (all[l] = true, all), {})).sort().join('');
}
function longest(s1, s2) {
const uniqueLetters = new Set(s1 + s2);
return [...uniqueLetters.values()].sort().join('');
}
function longest(s1, s2) {
const uniqueLetters = new Set(s1 + s2);
return [...uniqueLetters].sort().join('');
}
console.log(longest('aretheyhere', 'yestheyarehere'), 'aehrsty');
console.log(longest('loopingisfunbutdangerous', 'lessdangerousthancoding'), 'abcdefghilnoprstu');
console.log(longest('inmanylanguages', 'theresapairoffunctions'), 'acefghilmnoprstuy');