Home > Back-end >  Google Sheet add increment numbers for duplicates
Google Sheet add increment numbers for duplicates

Time:02-19

Hello how to get a increment numbers for duplicate with array function.

Example in below picture if we search a word in entire A row if there is any duplicates it will add numbers in B.

enter image description here

CodePudding user response:

Try

=arrayformula(if(A1:A="",,
A1:A&if(countif(A1:A,A1:A)=1,,
"-"&text(countifs(A1:A,A1:A,row(A1:A),"<="&row(A1:A)),"00"))))

enter image description here

Or...

=A1&if(countif(A$1:A,A1)=1,,
"-"&text(countif(A$1:A1,A1),"00")) 

...and dragdown.

enter image description here

CodePudding user response:

Count duplicates

function countdups() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const vs = sh.getRange(1,1,sh.getLastRow(),2).getValues();
  let uA = [];
  let obj = {pA:[]};
  vs.forEach(r => {
    if(!~uA.indexOf(r[0])) {
      uA.push(r[0]);
      obj[r[0]] = 1;
      obj.pA.push(r[0]);
    } else {
      obj[r[0]]  = 1;
    }
  });
  let vo = obj.pA.map(p => [`${p}-${obj[p]}`]);
  sh.getRange(1,2,vo.length,1).setValues(vo);
}
COL1 COL1-1
7 7-2
6 6-1
9 9-1
11 11-3
16 16-1
4 4-1
15 15-1
19 19-3
11 2-1
2 10-3
10 18-1
11 5-1
10 0-1
18
19
19
10
5
0
7
  • Related