Reputation: 690
I have the below array. Did console.log for the array and it is as below
(3) [Array(4), Array(4), Array(4)]
0 ["jackpot", "cherry", "bar", "pear"]
1 ["raspberry", "raspberry", "raspberry", "lemon"]
2 ["plum", "crown", "lemon", "jackpot"]
I tried the below and in console i just see my array and 1 next to it
var counts = {};
currentResults.forEach(function(obj) {
var key = JSON.stringify(obj)
counts[key] = (counts[key] || 0) + 1
})
console.log(counts);
How can i search and find the duplicated values in the above array/object and count how many duplicated items I have with javascript/jquery?
Upvotes: 1
Views: 2942
Reputation: 12181
Here you go with a solution
var data = [["jackpot", "cherry", "bar", "pear"], ["raspberry", "raspberry", "raspberry", "lemon"],["plum", "crown", "lemon", "jackpot"]];
var key = {};
for(var i=0; i<data.length; i++){
for(var j=0; j<data[i].length; j++){
if(typeof key[data[i][j]] === 'undefined'){
key[data[i][j]] = 1;
} else {
key[data[i][j]] = parseInt(key[data[i][j]]) + 1;
}
}
}
console.log(key);
I assumed your data as an array of array. Loop through both the array & for every item, kept an count in the form of a JSON
(key: value).
Key as item & value is the count.
Updated Solution to get the duplicate items
var data = [["jackpot", "cherry", "bar", "pear"], ["raspberry", "raspberry", "raspberry", "lemon"],["plum", "crown", "lemon", "jackpot"]];
var key = {};
var duplicate = [];
for(var i=0; i<data.length; i++){
for(var j=0; j<data[i].length; j++){
if(typeof key[data[i][j]] === 'undefined'){
key[data[i][j]] = 1;
} else {
key[data[i][j]] = parseInt(key[data[i][j]]) + 1;
if(parseInt(key[data[i][j]]) >= 3 && duplicate.indexOf(data[i][j]) == -1){
duplicate.push(data[i][j]);
}
}
}
}
console.log(duplicate);
Updated Solution for finding duplicates within child array
var data = [["jackpot", "cherry", "bar", "pear"], ["raspberry", "raspberry", "raspberry", "lemon"],["plum","jackpot", "crown", "lemon", "jackpot", "jackpot"]];
var key = {};
var duplicate = [];
for(var i=0; i<data.length; i++){
key = {};
for(var j=0; j<data[i].length; j++){
if(typeof key[data[i][j]] === 'undefined'){
key[data[i][j]] = 1;
} else {
key[data[i][j]] = parseInt(key[data[i][j]]) + 1;
if(parseInt(key[data[i][j]]) >= 3 && duplicate.indexOf(data[i][j]) == -1){
duplicate.push(data[i][j]);
}
}
}
}
console.log(duplicate);
Hope this will help you.
Upvotes: 1
Reputation: 304
You can check duplications for each arrays or you can merge arrays in one array for checking all arrays.
var array1 = ["jackpot", "cherry", "bar", "pear"];
var array2 = ["raspberry", "raspberry", "raspberry", "lemon"];
var array3 = ["plum", "crown", "lemon", "jackpot"];
var parentArray = [array1, array2, array3];
var joinedArray = [];
// ForEach Array
var duplicatedElementsForEachArray = [];
parentArray.forEach(function (childArray) {
// Merge Arrays in joinedArray
joinedArray = joinedArray.concat(childArray);
var duplicatedElements = 0;
var sortedChildArray = childArray.slice().sort(); // Sorting each Arrays
var i = 0, l = sortedChildArray.length - 1;
while (i < l) { if (sortedChildArray[i] == sortedChildArray[i + 1]) duplicatedElements++; i++; } // If i. and (i+1). elements are same, this will be duplication
duplicatedElementsForEachArray.push(duplicatedElements);
console.log(duplicatedElements);
});
// Joined Array, you can check duplications for merged array with same algorithm
var sortedJoinedArray = joinedArray.slice().sort();
var duplicatedElementsForJoinedArray = 0;
var i = 0, l = sortedJoinedArray.length - 1;
while (i < l) { if (sortedJoinedArray[i] == sortedJoinedArray[i + 1]) duplicatedElementsForJoinedArray++; i++; }
console.log(duplicatedElementsForJoinedArray);
Upvotes: 0
Reputation: 1496
use this :)
var test=[["jackpot", "cherry", "bar", "pear"], ["raspberry", "raspberry", "raspberry", "lemon"],["plum", "crown", "lemon", "jackpot"]];
var counts = {};
for(var i=0;i<test.length;i++)
test[i].forEach((x)=> { counts[x] = (counts[x] || 0)+1; });
console.log(counts);
Upvotes: 0
Reputation: 1521
You can iterate over each array element inside your main array and push unique values in a new array and if the value already exists than you got a duplicate and store it in result array.
Here I am not writing the whole code as I can not see what you have tried.
You can use this method to check if an element already exist in new array
arr.indexOf()
Edit: A better approach would be to iterate over each array element inside your main array and store them as a property of an object and their count as the value of the property. At the end your object would be something like this:
{
"raspberry": 3,
"lemon" : 2
}
Upvotes: 0