countfirst1.js
/*Count first
You have been given a sequence of n integers. Lets say the given sequence is a0, a1, a2, a3 ...
an-1.
You need to find the number of occurrences of the first value (i.e. a0) in the given sequence.
Input Format:
First line contains an integer n, denoting the length of the sequence.
Next n lines contains one integer each.
Output Format:
One integer, denoting the result, as mentioned above.
Example:
Input:
5
10
20
30
40
10
Output:
2
Explanation:
First line contains 5, meaning the sequence has 5 integers.
First element is 10.
In the given sequence, 10 occurs 2 times. So, the output is 2.
*/
let fs = require("fs");
let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');
function readLine() {
idx++;
return data[idx - 1].trim();
}
//======================================================================================================
let noOfInputs = parseInt(readLine()); // 5
let firstInteger = parseInt(readLine()); // 10
let count = 1;
for (let i = 1; i <= noOfInputs - 1; i++) {
let num = parseInt(readLine());
if (num == firstInteger) {
count++;
}
}
console.log(count);
///////////////////////////////////////////////////////////////////////////////////////////////
TERMINAL:

Comments
Post a Comment