fork(1) download
  1. /**
  2.  * Check if a string contains any character appearing exactly `count` times
  3.  * @param {string} line - The input string to be checked
  4.  * @param {number} [charCount=2] - The required number of occurrences for a character to be considered
  5.  * @returns {boolean} - True if any character appears exactly `charCount` times, false otherwise
  6.  */
  7. function hasExactChars(line = '', charCount = 2) {
  8. // Check if any character has the required count of occurrences
  9. return Object.values([...line].reduce(
  10. (acc, char) => ({ ...acc, [char]: (acc[char] || 0) + 1 }),
  11. {}
  12. )).some((count) => count === charCount);
  13. };
  14.  
  15. // Read input line by line using `readline` function
  16. // Check if each line has any character appearing exactly twice, and print it if so
  17. while (line = readline()) {
  18. if (hasExactChars(line, 2)) {
  19. print(line);
  20. }
  21. }
  22.  
Success #stdin #stdout 0.04s 17684KB
stdin
asdf
fdas
asds
d fm
dfaa
aaaa
aabb
aaabb
stdout
asds
dfaa
aabb
aaabb