Skip to content

Instantly share code, notes, and snippets.

@zeeshanalisyed
Created June 29, 2019 12:09
Show Gist options
  • Select an option

  • Save zeeshanalisyed/3b8ead5c61cfca8d81d308aea864cb69 to your computer and use it in GitHub Desktop.

Select an option

Save zeeshanalisyed/3b8ead5c61cfca8d81d308aea864cb69 to your computer and use it in GitHub Desktop.
this gist explains how to get number of pairs from an array
var array = [10, 20, 20, 10, 10, 30, 50, 10, 20, 4, 20, 30, 500, 500, 500];
const unique = (array) => array.filter((item, i, list) => list.indexOf(item) === i);
const countInArray = (array, what) => array.filter(item => item == what).length;
const keys = unique(arr)
let result = 0;
keys.map(key => {
var pair = Math.floor(countInArray(arr, key)/2);
if (pair > 0) {
result += pair;
}
});
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment