countSameValueZero

Count the number of elements in an array that are equal to a specified value.

Usage

var countSameValueZero = require( '@stdlib/array/base/count-same-value-zero' );

countSameValueZero( x, value )

Counts the number of elements in an array that are equal to a specified value.

var x = [ 0, 1, 0, 1, 2 ];

var out = countSameValueZero( x, 1 );
// returns 2

In contrast to an implementation using the strict equality operator ===, the function treats NaNs as the same value.

var x = [ NaN, NaN, NaN ];

var out = countSameValueZero( x, NaN );
// returns 3

In contrast to an implementation using the SameValue Algorithm (as specified in ECMAScript 5), the function does not distinguish between +0 and -0.

var x = [ 0.0, -0.0, 0.0 ];

var out = countSameValueZero( x, 0.0 );
// returns 3

Examples

var bernoulli = require( '@stdlib/random/array/bernoulli' );
var countSameValueZero = require( '@stdlib/array/base/count-same-value-zero' );

var x = bernoulli( 10, 0.5, {
    'dtype': 'generic'
});
console.log( x );

var n = countSameValueZero( x, 1 );
console.log( n );
Did you find this page helpful?