Problem 15. countfirst.js

 /*Question

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 lengthOfSeq = parseInt(readLine()); //5

let firstNumber = parseInt(readLine()); //10

let count = 1; // because 10 has been counted one time already

for (let i = 1; i < lengthOfSeq; i++) {

let integers = parseInt(readLine()); // this will read the other integers

if (integers == firstNumber) {
count++;

}

}

console.log(count);

Terminal:
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node countfirst.js <input.txt 2 Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$

Comments

Popular posts from this blog

Problem 33. patter122333.js

Problem 38. diamond.js

Problem 16. chocolatebills.js