Created
June 29, 2019 12:09
-
-
Save zeeshanalisyed/3b8ead5c61cfca8d81d308aea864cb69 to your computer and use it in GitHub Desktop.
this gist explains how to get number of pairs from an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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