I want to display my duplicate word in another array and how many times it is repeated.
For example, I have in my array apple | apple | apple | bannane
. I want to display in my next array: apple | 3 | bannane | 1
My code so far:
function suppd() {
var monSpread = SpreadsheetApp.getActiveSheet();
var myObject = monSpread.getDataRange().getValues();
var nvData = [];
for(i in myObject){
var row = myObject[i];
var duplicate = false;
for(j in nvData){
if(row.join() == nvData[j].join()){
duplicate = true;
}
}
if(!duplicate){
nvData.push(row);
}
}
monSpread.getRange(1, 3, nvData.length, nvData[0].length).setValues(nvData);
}
It would be better to use an object to record what items are duplicates, and what the count is. You are already checking for NOT
a duplicate:
Logger.log("duplicate value is: " + duplicat);
if(!duplicate){
nvData.push(row);
}
Add and ELSE condition:
var dupObject = {};
var countThisDuplicate = 0;
if(!duplicate){
nvData.push(row)
} else {
//Check if duplicate is already in the object
Logger.log("The else part of the code ran!");
if (dupObject[j]) {
//If duplicate is already in the object, get the count, then add one to it
countThisDuplicate = dupObject[j] + 1;
dupObject[j] = countThisDuplicate;
} else {
//If duplicate is NOT already in the object, add the member with a count of one
dupObject[j] = 1;
};
};
I just threw this together and didn't test it, so it probably has errors. It's just to give you an idea, and try to answer your question. Hopefully it gives you the basic logic to solve your problem.