Skip to content

Instantly share code, notes, and snippets.

@evandertino
Created July 30, 2019 09:27
Show Gist options
  • Save evandertino/2e05112bf5b14286a882bb5fa237c36f to your computer and use it in GitHub Desktop.
Save evandertino/2e05112bf5b14286a882bb5fa237c36f to your computer and use it in GitHub Desktop.
A function that given an array of integers, it return a map of integers that appear multiple times and the number of times it appears.
const mapOfIntergers = arrayOfInts => {
let results = {};
for (let i = 0; i < arrayOfInts.length; i++) {
let m = arrayOfInts[i];
let count = arrayOfInts.filter(dupInt => dupInt === m).length;
if (count > 1) results[m] = count;
}
return results;
};
console.log('mapOfIntergers', mapOfIntergers([1, 2, 3, 4, 6, 6, 7, 8, 9, 5, 2, 6, 1, 8]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment