-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathstrings.js
More file actions
31 lines (23 loc) · 759 Bytes
/
strings.js
File metadata and controls
31 lines (23 loc) · 759 Bytes
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
/* Given a contiguous sequence of numbers in which each number repeats thrice,
there is exactly one missing number. Find the missing number.
eg: 11122333 : Missing number 2
11122233344455666 Missing number 5
*/
//Create a hashmap
//add all numbers to hashmap.
// go through hashmap and return object with less than 3 frequency
const findMissingNumber = (inputString, maxFrequency) => {
const hashMap = {};
[...inputString].forEach( (i) => {
if(hashMap[i]) {
hashMap[i] = Number(hashMap[i]) + 1;
}
else {
hashMap[i] = 1;
}
});
for (const key of Object.keys(hashMap)) {
if(hashMap[key] < maxFrequency) return key;
}
}
console.log(findMissingNumber('11122233344455666', 3));