-
Notifications
You must be signed in to change notification settings - Fork 4
/
Counting Problem Again.js
47 lines (45 loc) · 1.2 KB
/
Counting Problem Again.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
//count characters inn string also take input and print along with frequency in alphabatical order
function runProgram(input) {
var m = new Map();
var alpha = "abcdefghijklmnopqrstuvwxyz";
for (i = 0; i < alpha.length; i++) {
if (m.get(alpha[i]) == undefined) {
m.set(alpha[i], 0)
}
}
for (i = 0; i < input.length; i++) {
if (m.get(input[i]) == 0) {
m.set(input[i], 1)
}
else {
var curr = m.get(input[i])
m.set(input[i], (curr + 1));
}
}
for (var [key, value] of m) {
if (m.get(key) > 0) {
console.log(key + " " + m.get(key));
}
}
//console.log(m)
}
if (process.env.USER === "") {
runProgram(``);
} else {
process.stdin.resume();
process.stdin.setEncoding("ascii");
let read = "";
process.stdin.on("data", function (input) {
read += input;
});
process.stdin.on("end", function () {
read = read.replace(/\n$/, "");
read = read.replace(/\n$/, "");
runProgram(read);
});
process.on("SIGINT", function () {
read = read.replace(/\n$/, "");
runProgram(read);
process.exit(0);
});
}