Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const INPUT_FILE = 'input-201.txt'
- const OUTPUT_FILE = 'input-201.a.txt'
- const file = fs.readFileSync(INPUT_FILE, 'utf8');
- const array = file.split('').map(el => el).filter(el => el !== '\n');
- const unmatched = [];
- const _count = (arr, number) => arr.reduce((pre, cur) => (cur === number) ? ++pre : pre, 0);
- const arrayFormat = arr => arr.sort().map((el) => {return {el, count: _count(arr, el)}});
- const findUnPaired = final => final.filter(data => {
- if (data.count === 1) {
- return data.el
- }
- });
- const format = (found) => {
- if (found) {
- const lastIndex = found.slice(-1);
- if (lastIndex[0].length) {
- return lastIndex[0][0].el
- }
- }
- }
- const formated = arrayFormat(array);
- const found = findUnPaired(formated);
- unmatched.push(found);
- fs.writeFile(OUTPUT_FILE, format(unmatched), function (err) {
- if (err) return console.log(err);
- });
Advertisement
Add Comment
Please, Sign In to add comment