@stdlib/array-base-count-same-value v0.2.0
countSameValue
Count the number of elements in an array that are equal to a specified value.
Installation
npm install @stdlib/array-base-count-same-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 );
Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2024. The Stdlib Authors.