fork download
  1. /**
  2.  * Counts the occurrences of each character in a given string.
  3.  * @param {string} line - The input string to count characters from.
  4.  * @returns {object} - An object containing the counts of each character.
  5.  */
  6. function getCharCounts(line = '') {
  7. return line
  8. .split('')
  9. .reduce((acc, char) => ({ ...acc, [char]: (acc[char] || 0) + 1 }), {});
  10. }
  11.  
  12. // Read input line by line using the readline function
  13. // Check if any character in each line appears exactly twice, and print the line if so
  14. while ((line = readline())) {
  15. const charCounts = getCharCounts(line);
  16.  
  17. if (Object.values(charCounts).some((count) => count === 2)) {
  18. print(line);
  19. }
  20. }
  21.  
Success #stdin #stdout 0.03s 19648KB
stdin
asdf
fdas
asds
d fm
dfaa
aaaa
aabb
aaabb
stdout
asds
dfaa
aabb
aaabb