Home > Software engineering >  Formula to sort and filter 4 digit numbers with 2 or more of the same digits
Formula to sort and filter 4 digit numbers with 2 or more of the same digits

Time:05-12

Id like to use a filter and sort formula to filter out and sort all numbers that have two or more of the same number. they are all 4 digit numbers.

0055  
0121  
1134  
2352  
3411

this is just an example list of the output I would need. Thank you

CodePudding user response:

Replace numList with the range you're sorting on. (Or use numList as a named range for your input set.)

=SORT(FILTER(numList,
  (LEN(SUBSTITUTE(numList,LEFT(numList,1),""))<>3)
   (LEN(SUBSTITUTE(numList,MID(numList,2,1),""))<>3)
   (MID(numList,3,1)=RIGHT(numList,1))))

Filter conditions:

  1. (LEN(SUBSTITUTE(numList,LEFT(numList,1),""))<>3) Checks if the first digit is repeated.
  2. (LEN(SUBSTITUTE(numList,MID(numList,2,1),""))<>3) Checks if the second digit is repeated.
  3. (MID(numList,3,1)=RIGHT(numList,1)) Checks if digits 3 and 4 are the same. We can take this shortcut because we've already checked the first two digits at this point.

If any of these is true, then we have a repeating digit.

  • Related