Guest User

Untitled

a guest
Jun 8th, 2020
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. const fs = require('fs');
  2.  
  3. const INPUT_FILE = 'input-201.txt'
  4. const OUTPUT_FILE = 'input-201.a.txt'
  5.  
  6. const file = fs.readFileSync(INPUT_FILE, 'utf8');
  7.  
  8. const array = file.split('').map(el => el).filter(el => el !== '\n');
  9.  
  10. const unmatched = [];
  11.  
  12. const _count = (arr, number) => arr.reduce((pre, cur) => (cur === number) ? ++pre : pre, 0);
  13.  
  14. const arrayFormat = arr => arr.sort().map((el) => {return {el, count: _count(arr, el)}});
  15.  
  16. const findUnPaired = final => final.filter(data => {
  17. if (data.count === 1) {
  18. return data.el
  19. }
  20. });
  21.  
  22. const format = (found) => {
  23. if (found) {
  24. const lastIndex = found.slice(-1);
  25. if (lastIndex[0].length) {
  26. return lastIndex[0][0].el
  27. }
  28. }
  29. }
  30.  
  31.  
  32. const formated = arrayFormat(array);
  33. const found = findUnPaired(formated);
  34. unmatched.push(found);
  35.  
  36. fs.writeFile(OUTPUT_FILE, format(unmatched), function (err) {
  37. if (err) return console.log(err);
  38. });
Advertisement
Add Comment
Please, Sign In to add comment