We would like to know how to find the number of occurrences for a given value in an array.
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
var dataset = [2,2,4,2,6,4,7,8];
function findOccurrences(arr, val) {<!-- w ww . j a v a 2 s . c om-->
var i, j,
count = 0;
for (i = 0, j = arr.length; i < j; i++) {
(arr[i] === val) && count++;
}
return count;
}
document.writeln(findOccurrences(dataset, 2));
</script>
</head>
<body>
</body>
</html>
The code above is rendered as follows: