fork download
  1.  
  2. function hasCharWithExactlyTwoOccurrences(line) {
  3. const charCounts = Array(256).fill(0);
  4.  
  5. for (let i = 0; i < line.length; i++) {
  6. charCounts[line.charCodeAt(i)]++;
  7. }
  8.  
  9. for (let i = 0; i < 256; i++) {
  10. if (charCounts[i] === 2) {
  11. return true;
  12. }
  13. }
  14.  
  15. return false;
  16. }
  17.  
  18. function filterLines(lines) {
  19. const result = [];
  20. for (let i = 0; i < lines.length; i++) {
  21. if (hasCharWithExactlyTwoOccurrences(lines[i])) {
  22. result.push(lines[i]);
  23. }
  24. }
  25. return result;
  26. }
  27.  
  28. // Example input
  29. const input = [
  30. "asdf",
  31. "fdas",
  32. "asds",
  33. "d",
  34. "fm",
  35. "dfaa",
  36. "aaaa",
  37. "aabb",
  38. "aaabb"
  39. ];
  40.  
  41. const result = filterLines(input);
  42. console.log(result);
  43.  
Success #stdin #stdout 0.05s 17488KB
stdin
Standard input is empty
stdout
asds,dfaa,aabb,aaabb