countSameValue
Count the number of elements in an array that are equal to a specified value.
Usage
var countSameValue = require( '@stdlib/array/base/count-same-value' );
countSameValue( 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 = countSameValue( x, 1 );
// returns 2
In contrast to an implementation using the strict equality operator ===
, the function distinguishes between +0
and -0
and treats NaNs
as the same value.
var x = [ NaN, NaN, NaN ];
var out = countSameValue( x, NaN );
// returns 3
Notes
- The function uses the SameValue Algorithm as specified in ECMAScript 5.
Examples
var bernoulli = require( '@stdlib/random/array/bernoulli' );
var countSameValue = require( '@stdlib/array/base/count-same-value' );
var x = bernoulli( 10, 0.5, {
'dtype': 'generic'
});
console.log( x );
var n = countSameValue( x, 1 );
console.log( n );