Nodejs Array Item Occurrence numberOfOccurrences(item)

Here you can find the source of numberOfOccurrences(item)

Method Source Code

// http://www.codewars.com/kata/number-of-occurrences/javascript

Array.prototype.numberOfOccurrences = function(item) {
var drr = []; // Empty array
var i = 0 ;/*from   www.j  av a2  s  .  co  m*/
   for(i = 0 ; i < this.length ; i++) {
       if(item=== this[i]){
             drr.push(this[i]); // looping through the item and pushing the same number of occurrences into the empty array.
       }
   }
    return drr.length;

}

Related

  1. numberOfOccurrences(chr)
    Array.prototype.numberOfOccurrences = function(chr) {
      return this.filter(function(x){ return x == chr; }).length;
    
  2. numberOfOccurrences(desVal)
    Array.prototype.numberOfOccurrences = function(desVal) {
      var counter = 0;
      this.forEach(function(value){
        if(value === desVal){
          counter++;
      });
      return counter;
    };
    ...
    
  3. numberOfOccurrences(el)
    Array.prototype.numberOfOccurrences = function (el) {
      var count = 0;
      this.forEach(function (item) {
        if (item === el) count++;
      });
      return count;
    
  4. numberOfOccurrences(elem)
    Array.prototype.numberOfOccurrences = function(elem) {
      let obj = {};
      this.forEach(elem => elem in obj ? obj[elem]++ : obj[elem] = 1);
      return obj[elem] === undefined ? 0 : obj[elem]
    
  5. numberOfOccurrences(i)
    Array.prototype.numberOfOccurrences = function(i) {
      var total = 0;
      this.filter(function (x) {
        if (i === x)
          total++;
      });
      return total;
    };
    var arr = [0,1,2,2,3];
    ...
    
  6. numberOfOccurrences(item)
    var arr = [4, 0, 4];
    Array.prototype.numberOfOccurrences = function(item) {
      tmp = 0;
      for(var i=0; i<this.length; i++){
        if (item == this[i]) {
          tmp ++;
      console.log(tmp);
    ...
    
  7. numberOfOccurrences(n)
    Array.prototype.numberOfOccurrences = function(n) {
      var counter = 0;
      for (i = 0; i < this.length; i++){
        if (n == this[i]){
          counter++;
      return counter;
    
  8. numberOfOccurrences(num)
    Array.prototype.numberOfOccurrences = function(num) {
        return this.filter(function(item){return item==num;}).length;
    
  9. numberOfOccurrences(num)
    Array.prototype.numberOfOccurrences = function (num) {
      var output = 0;
      for (var i=0; i<this.length; i++) {
        if (this[i] === num) {
          output++;
      return output;
    };
    ...