34
\$\begingroup\$

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]
\$\endgroup\$
2
  • \$\begingroup\$ Are we allowed to take the length of the input list as one of the inputs? \$\endgroup\$
    – user96495
    Oct 12, 2020 at 9:02
  • 3
    \$\begingroup\$ @2x-1 No, unless you can't compute it from the list in your language. \$\endgroup\$
    – Zgarb
    Oct 12, 2020 at 9:15

36 Answers 36

1
2
1
\$\begingroup\$

PHP 63 Bytes

Number provided in $n, list provided in $a,

$p=explode($n,$a,2);echo$p[0].$n.str_replace("$n,", '', $p[1]);

Ungolfed:

$p = explode($n,$a,2);
echo $p[0].$n.str_replace("$n,", '', $p[1]);

e.g.

$n=432;
$a="[432,567,100,432,100]";
$p = explode($n,$a,2);
echo $p[0].$n.str_replace("$n,", '', $p[1]);

(I'm unsure if it's ok not to count the input into the bytes, or the opening '<?php' for that matter...)

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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.

How

  • 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.
\$\endgroup\$
1
\$\begingroup\$

Uiua SBCS, 6 bytes

▽↥⊃◰≠,

Try it!

\$\endgroup\$
1
\$\begingroup\$

jq, 58 bytes

def r(x):index(x)as$i|if$i then.[:$i+1]+(.[$i+1:]-[x])end;

Works with jq (the C implementation) and with gojq (the Go implementation).

Note that but for Code Golf, one would normally write $x instead of x.

Try it online: https://jqplay.org/s/pKEk1AdpPaB

\$\endgroup\$
0
\$\begingroup\$

Wolfram Language (Mathematica), 50 bytes

Insert[DeleteCases@##,#2,#&@@Position@##]~Check~#&

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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