# How to count duplicate value in an array in javascript

Asked  7 Months ago    Answers:  5   Viewed   30 times

Currently, I got an array like that:

``````var uniqueCount = Array();
``````

After a few steps, my array looks like that:

``````uniqueCount = [a,b,c,d,d,e,a,b,c,f,g,h,h,h,e,a];
``````

How can I count how many a,b,c are there in the array? I want to have a result like:

``````a = 3
b = 1
c = 2
d = 2
``````

etc.

## Answers

98

``````function count() {
array_elements = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e", "a"];

array_elements.sort();

var current = null;
var cnt = 0;
for (var i = 0; i < array_elements.length; i++) {
if (array_elements[i] != current) {
if (cnt > 0) {
document.write(current + ' comes --> ' + cnt + ' times<br>');
}
current = array_elements[i];
cnt = 1;
} else {
cnt++;
}
}
if (cnt > 0) {
document.write(current + ' comes --> ' + cnt + ' times');
}

}

count();``````

Demo Fiddle

You can use higher-order functions too to do the operation. See this answer

Tuesday, June 1, 2021

answered 7 Months ago
30

Use array_unique().

Example:

``````\$array = array(1, 2, 2, 3);
\$array = array_unique(\$array); // Array is now (1, 2, 3)
``````
Wednesday, March 31, 2021

answered 9 Months ago
60

Credit goes to this answer for the actual rotation method.

My method was pretty straightforward. Just determine what the row length was, and then iterate through each item, converting the array index to x/y equivalents and then apply the method used in the linked answer to rotate. Finally I converted the rotated X/Y coordinates back to an array index.

``````var grid = [
[0,0], [0,1], [0,2], [0,3],
[1,0], [1,1], [1,2], [1,3],
[2,0], [2,1], [2,2], [2,3],
[3,0], [3,1], [3,2], [3,3]
];

var newGrid = [];
var rowLength = Math.sqrt(grid.length);
newGrid.length = grid.length

for (var i = 0; i < grid.length; i++)
{
//convert to x/y
var x = i % rowLength;
var y = Math.floor(i / rowLength);

//find new x/y
var newX = rowLength - y - 1;
var newY = x;

//convert back to index
var newPosition = newY * rowLength + newX;
newGrid[newPosition] = grid[i];
}

for (var i = 0; i < newGrid.length; i++)
{
console.log(newGrid[i])
}
``````

The output:

``````[3, 0] [2, 0] [1, 0] [0, 0]
[3, 1] [2, 1] [1, 1] [0, 1]
[3, 2] [2, 2] [1, 2] [0, 2]
[3, 3] [2, 3] [1, 3] [0, 3]
``````

Fiddle for the lazy. And a 5x5 grid fiddle to demonstrate that the algorithm works for N grid sizes as long as they are square.

Wednesday, June 23, 2021

answered 6 Months ago
85

Usage: `isEqArrays(arr1, arr2)`

``````//
// Array comparsion
//

function inArray(array, el) {
for ( var i = array.length; i--; ) {
if ( array[i] === el ) return true;
}
return false;
}

function isEqArrays(arr1, arr2) {
if ( arr1.length !== arr2.length ) {
return false;
}
for ( var i = arr1.length; i--; ) {
if ( !inArray( arr2, arr1[i] ) ) {
return false;
}
}
return true;
}
``````
Thursday, July 29, 2021

answered 5 Months ago
74

There is `Array.filter()`:

``````var numbers = [1, 2, 3, 4, 5];
var filtered = numbers.filter(function(x) { return x > 3; });

// As a JavaScript 1.8 expression closure
filtered = numbers.filter(function(x) x > 3);
``````

Note that `Array.filter()` is not standard ECMAScript, and it does not appear in ECMAScript specs older than ES5 (thanks Yi Jiang and jAndy). As such, it may not be supported by other ECMAScript dialects like JScript (on MSIE).

Nov 2020 Update: Array.filter is now supported across all major browsers.

Saturday, September 4, 2021

answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
Share
Related Answers
Top Answers Related To