fork download
  1. const readline = require('readline');
  2.  
  3. // Create input interface
  4. const rl = readline.createInterface({
  5. input: process.stdin,
  6. output: process.stdout
  7. });
  8.  
  9. // Store all input lines
  10. const inputLines = [];
  11.  
  12. // Greet user and give instructions
  13. console.log('Welcome! Please enter lines of text (one per line).');
  14. console.log('When done, press Ctrl+D (Linux/macOS) or Ctrl+C (Windows) or Ctrl+Z then Enter (Windows).');
  15.  
  16. // Store each line
  17. rl.on('line', (line) => {
  18. inputLines.push(line.trim());
  19. });
  20.  
  21. // When input ends, process and show output
  22. rl.on('close', () => {
  23. const output = [];
  24.  
  25. for (const line of inputLines) {
  26. const freq = {};
  27.  
  28. for (const char of line) {
  29. if (char !== ' ') {
  30. freq[char] = (freq[char] || 0) + 1;
  31. }
  32. }
  33.  
  34. const hasExactlyTwo = Object.values(freq).some(count => count === 2);
  35.  
  36. if (hasExactlyTwo) {
  37. output.push(line);
  38. }
  39. }
  40.  
  41. console.log('\nOutput:');
  42. console.log(output.join('\n'));
  43. });
Success #stdin #stdout 0.07s 42128KB
stdin
Standard input is empty
stdout
Welcome! Please enter lines of text (one per line).
When done, press Ctrl+D (Linux/macOS) or Ctrl+C (Windows) or Ctrl+Z then Enter (Windows).

Output: