Question: I am working on this kata <https://www.codewars.com/kata/count-9-s-from-1-to-n/train/javascript>

and i have written this code for it, but its not working. This question is similar to this one [Count the number of occurrences of 0's in integers from 1 to N](https://stackoverflow.com/questions/11891984/count-the-number-of-occurrences-of-0s-in-integers-from-1-to-n) 
but it is different because searching for 9's is practically very different to searching for 0's.
think part of the problem with this code is that it takes too long to run...
any advice appreciated!

```
function has9(n) {

  var nine = [];

  var ninearr = n.toString().split('');

  for (var j = 0; j < ninearr.length; j++) {
    if (ninearr[j] == '9') {

      nine.push(ninearr[j]);
    }
  }
  return nine.length;
}

function number9(n) {
  var arr = [];
  var arrnew = [];

  for (var i = 0; i <= n; i++) {
    arr.push(i);
  }

  for (var l = 0; l < arr.length; l++) {
    arrnew.push(has9(l));

  }

  var sum = arrnew.reduce((a, b) => a + b, 0);
  return sum;
}

```

Answer: 
delete the minor

```
DELETE A
(
   SELECT 
       [Column1PK], [Column2], [Column3],
       RN = ROW_NUMBER() OVER (PARTITION BY [Column1PK] ORDER BY [Column1PK] DESC)
   FROM 
       [dbo].[table] 
) A
WHERE RN > 1

```

Delete the largest

```
DELETE A
(
   SELECT 
       [Column1PK], [Column2], [Column3],
       RN = ROW_NUMBER() OVER (PARTITION BY [Column1PK] ORDER BY [Column1PK] ASC)
   FROM 
       [dbo].[table] 
) A
WHERE RN > 1

```
Not sure if I understand you correctly, but if you want to remove only the duplicates with the highest value, you can use exists. That would look something like this:

```
DELETE FROM Table_A AS A
WHERE EXISTS (SELECT * FROM Table_A AS A2
               WHERE A2.Column1PK = A.Column1PK
                 AND A2?? < A.??)

```