The task

In this challenge, you are given a number and a list. Your task is to remove from the list all occurrences of the given number except the first (leftmost) one, and output the resulting list. The other elements of the list should be left intact.

  • The number will be a positive integer below 1000, and the list will only contain positive integers below 1000.
  • The list is not guaranteed to contain any occurrences of the given number. It may even be empty. In these cases you should output the list as-is.
  • Input and output formats are flexible within reason. You can output by modifying the list in place.
  • The lowest byte count wins.

Test cases

5 [] -> []
5 [5] -> [5]
5 [5,5] -> [5]
10 [5,5] -> [5,5]
10 [5,5,10,10,5,5,10,10] -> [5,5,10,5,5]
2 [1,2,3,1,2,3,1,2,3] -> [1,2,3,1,3,1,3]
7 [9,8,7,6,5] -> [9,8,7,6,5]
7 [7,7,7,7,7,7,7,3,7,7,7,7,7,7,3,7,1,7,3] -> [7,3,3,1,3]
432 [432,567,100,432,100] -> [432,567,100,100]
  • \$\begingroup\$ Are we allowed to take the length of the input list as one of the inputs? \$\endgroup\$ – user96495 Oct 12 '20 at 9:02
  • 3
    \$\begingroup\$ @2x-1 No, unless you can't compute it from the list in your language. \$\endgroup\$ – Zgarb Oct 12 '20 at 9:15

34 Answers 34


Google Sheets, 72

  • Column A is input array. Name range A.
  • B1 is target value. Name range B.
  • C1 is =IFNA(MATCH(B,A,
  • D1 is =ArrayFormula(IF((ROW(A)>C1)*(A=B),,A. Name range D.
  • E1 is =IFNA(FILTER(D,D

+3 total for named ranges.


  • C1 looks for the index of the occurrence. If none is found, return blank (0-like).
  • D1 filters out values if 1) Row is greater than the first occurrence's index and 2) if the value matches.
  • E1 Removes blanks. If D is empty, it's NA, so just output a blank.

k4, 12 bytes

  • 1_&x=y identify indices of duplicates, ignoring the first.
  • y_/| drop values at the specified indices (reversing the indices as this is a "somewhat" inplace operation, in that the list being dropped from shrinks as items are dropped)

An ngn/k version mostly works, except when passed in an empty list. In k4, an empty list is handled, returning an empty list ().


Wolfram Language (Mathematica), 50 bytes


Try it online!


T-SQL, 49 bytes

Input is an integer and a temporary table

SELECT v FROM # GROUP BY v,i*(v-@)ORDER BY min(i)

Try it online


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.