Skip to content

Instantly share code, notes, and snippets.

@jaxxreal
Created February 12, 2020 09:51
Show Gist options
  • Save jaxxreal/c73d4d6e93f577be83c7df33f2fc2e1e to your computer and use it in GitHub Desktop.
Save jaxxreal/c73d4d6e93f577be83c7df33f2fc2e1e to your computer and use it in GitHub Desktop.
function makePercent(inputArr) {
const sum = inputArr.reduce((sum, next) => sum + parseFloat(next), 0);
return inputArr.map(v => (parseFloat(v) / sum * 100).toFixed(3));
}
// time complexity O(2n)
// space complexity O(n+1)
// array length must be lower than 10.000.000 (10 millions) to make it run under 5s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment