I want to find Count Of specific element in array using JavaScript for example [2,2,2,3,3,3,3,3] count of 2 is 3 and count of 3 is 5.
Asked
Active
Viewed 278 times
7 Answers
1
var map = {};
var arr = [2, 2, 2, 3, 3, 3, 3, 3];
for (var i = 0; i < arr.length; i++) {
if (map[arr[i]]) {
map[arr[i]]++;
} else {
map[arr[i]] = 1;
}
}
for(var key in map){
console.log("occurence of "+ key+" = "+map[key]);
}
Output:
occurence of 2 = 3
occurence of 3 = 5

vikrant singh
- 2,091
- 1
- 12
- 16
-
1Depending on your state of mind, `map[arr[i]] = (map[arr[i]] || 0) + 1;` is more compact. – Sep 04 '14 at 16:54
0
Just for fun (works only with strings and numbers).
[2,2,2,3,3,3,3,3].join().match(/3/g).length; // return 5

Novelist
- 469
- 4
- 15
-
only works for single-digit integers: [2,2,2,3,3,3,3,33].join().match(/3/g).length is 6 and not 4 – SheetJS Oct 21 '14 at 14:33
0
Something like this?
function searchInArray(someInt, someArray){
var count = 0;
for(var i in someArray){
if(someArray[i] === someInt){
count++;
}
}
return count;
}
searchInArray(2, [2,2,2,2,2,3,4,4,4,5]);
//returns 5

Martinspire
- 591
- 1
- 5
- 16
0
In pure JavaScript:
function countOccurrences(array) {
var occurrences = {};
for (var i = 0, l = array.length; i < l; i++) {
if (occurrences[array[i]] === undefined) {
occurrences[array[i]] = 1;
}
else {
occurrences[array[i]]++;
}
}
return occurrences;
}
var test = [2,2,2,2,3,3,3,3,3,4,4,5];
console.log(countOccurrences(test));

Jean-Karim Bockstael
- 1,385
- 9
- 18
0
Use a loop.
var thisChar;
var thisCharCount;
for (var i = 0; i < yourArray.length; i++) {
if(yourArray[i] == thisChar)
thisCharCount++;
}
Depending if you compare to set or dynamic comparable values, use a array.

Jeffrey Wong
- 41
- 1
- 4
0
Use Underscore:
_.groupBy([2,2,2,3,3,3,3,3])
> {2: [2,2,2], 3: [3,3,3,3,3]}
Then count the length of the arrays.
Do it yourself using Array#reduce
:
function counts(array) {
return array.reduce(function(prev, current) {
prev[current] = (prev[current] || 0) + 1;
return prev;
}, {});
}
> counts([2,2,2,3,3,3,3,3]) // {2: 3, 3: 5}
To count occurrences of a specific val
:
function count(array, val) {
for (var i=0, index=0; (index = array.indexOf(val, index)) !==-1; i++) { }
return i;
}
Or if you prefer
function count(array, val) {
var i=0, index=0;
while ((index = array.indexOf(val, index)) !== -1) { i++; }
return i;
}
Or perhaps recursively?
function count(array, val) {
var index = array.indexOf(val);
return index === -1 ? 0 : count(array.slice(index), val) + 1;
}
0
Why not using jQuery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
var count2 = 0, count3 = 0;
$.each([2,2,2,3,3,3,3,3], function( index, value ) {
if(value==2)
count2++;
if(value==3)
count3++;
});
alert("2:"+count2);
alert("3:"+count3);
});

New Man
- 1
- 1
-
Yes, only jQuery can do loops. By the way, `type=javascript` is not necessary. See http://stackoverflow.com/questions/5265202/do-you-need-text-javascript-specified-in-your-script-tags. – Sep 04 '14 at 12:03