Skip to content

Instantly share code, notes, and snippets.

@codermapuche
Created March 20, 2025 21:48
Show Gist options
  • Save codermapuche/c53b7d9181663e675527241f0cc3ecf4 to your computer and use it in GitHub Desktop.
Save codermapuche/c53b7d9181663e675527241f0cc3ecf4 to your computer and use it in GitHub Desktop.
"use strict";
// ----------------------------------------------------------------------------
const fs = require('fs');
// ----------------------------------------------------------------------------
const INPUT = 'palabrasRI.txt',
CRLF = '\n';
// ----------------------------------------------------------------------------
let inputs = fs.readFileSync(INPUT, 'utf8');
inputs = inputs.split(CRLF);
inputs = inputs.reduce((acc, w) => {
if ( !acc.hasOwnProperty(w) ) {
acc[w] = { w, c: 0 };
}
acc[w].c++;
return acc;
}, { });
inputs = Object.values(inputs);
inputs.sort((a, b) => (a.c - b.c));
console.log('Cantidad de palabras únicas', inputs.length);
if ( inputs.length > 0 ) {
console.log('Cantidad de veces que aparece cada palabra', inputs);
console.log('Palabra menos frecuente', inputs[0].w);
console.log('Palabra más frecuente', inputs[inputs.length - 1].w);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment