Javascript Array numberOfOccurrences(elem)

Description

Javascript Array numberOfOccurrences(elem)


Array.prototype.numberOfOccurrences = function(elem) {
  let obj = {};/*from  w  ww  .j  a  v a2 s.  c  o m*/
  this.forEach(elem => elem in obj ? obj[elem]++ : obj[elem] = 1);
  return obj[elem] === undefined ? 0 : obj[elem]
}



PreviousNext

Related