0
votes

I found the below google-app-script online which finds duplicates in the specified range of rows in a google spreadsheet. I need to edit this script in such a way that if the duplicates occur more than 2 times it should show those values.

This is how the script looks like:

function dups(rows) {
  var values = {};
  var duplicates = [];
  for (var i = 0; i < rows.length; i++) {
    var value = rows[i][0];
    if (values[value] !== undefined && duplicates.indexOf(value) == -1) {
      duplicates.push(value);
    } else {
      values[value] = true
    }
  }
  return duplicates;
}

For example with this script if i type =dups(A1:A30)in any cell i get the list of unique values which are repeated more than once . But i want values which are repeated more than twice.

Thanks in advance, drc

1

1 Answers

2
votes

With triples unfortunately you can't just to a (simple) indexOf so the most efficient way is to count occurrences and stop when we hit two.

function trips(rows) {
  var output = [];
  var appearances;

  for (var row = 0; row < rows.length-2; row++) {
    var value = rows[row][0];

    if (output.indexOf(value) > -1) {
      continue;
    }

    appearances = 0;
    for (var row2 = row + 1; row2 < rows.length; row2++) {
      if (value === rows[row2][0]) {
        appearances += 1;
      }
      if (appearances >= 2) {
        output.push(value);   
        break;
      }
    }  
  }
  return output;
}