Nodejs Array Item Occurrence numberOfOccurrences(num)

Here you can find the source of numberOfOccurrences(num)

Method Source Code

// The numberOfOccurrences function must return the number of occurrences of
// an element in an array.

// var arr = [0,1,2,2,3];
// arr.numberOfOccurrences(0) === 1;
// arr.numberOfOccurrences(4) === 0;
// arr.numberOfOccurrences(2) === 2;
// arr.numberOfOccurrences("a") === 0;

Array.prototype.numberOfOccurrences = function (num) {
  var output = 0;
  for (var i=0; i<this.length; i++) {
    if (this[i] === num) {
      output++;//w  w w  . ja  v a  2 s.c o m
    }
  }
  return output;
};

console.log([4,0,4].numberOfOccurrences(4), 2);

Related

  1. 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];
    ...
    
  2. numberOfOccurrences(item)
    Array.prototype.numberOfOccurrences = function(item) {
    var drr = []; 
    var i = 0 ;
       for(i = 0 ; i < this.length ; i++) {
           if(item=== this[i]){
               drr.push(this[i]); 
        return drr.length;
    ...
    
  3. 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);
    ...
    
  4. numberOfOccurrences(n)
    Array.prototype.numberOfOccurrences = function(n) {
      var counter = 0;
      for (i = 0; i < this.length; i++){
        if (n == this[i]){
          counter++;
      return counter;
    
  5. numberOfOccurrences(num)
    Array.prototype.numberOfOccurrences = function(num) {
        return this.filter(function(item){return item==num;}).length;
    
  6. numberOfOccurrences(number)
    Array.prototype.numberOfOccurrences = function(number) {
      return this.reduce(function (previous, current) {
        return (current === number) ? ++previous : previous;
      }, 0);
    };
    
  7. numberOfOccurrences(search)
    Array.prototype.numberOfOccurrences = function (search) {
      return this.filter(function (num) { return search === num }).length;
    
  8. numberOfOccurrences(search)
    Array.prototype.numberOfOccurrences = function(search) {
      return this.filter( function(num){ return search === num } ).length;
    
  9. numberOfOccurrences(target)
    Array.prototype.numberOfOccurrences = function(target) {
      for(var i =0;i<this.length;i++){
        var x = this.filter(function(b){return b === target}).length
      return x;