33
\$\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 '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

10
\$\begingroup\$

Husk, 5 4 bytes

üoEė

Try it online!

  • Saved 1 byte thanks to Razetime
  • Saved 2 bytes thanks to Jo King

Thanks to Razetime for suggesting ü and Jo King for letting me know I could leave the superscript arguments out, saving me 2 bytes. It removes duplicates with a custom predicate that makes sure both arguments are equal to the number to be removed.

Explanation:

üoEė
ü     Remove duplicates by binary function (implicit second argument)
 o    Compose 2 functions
   ė  Make a list of 3 elements (first element is implicitly added)
  E   Are they all equal?
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Ë= can just be E \$\endgroup\$
    – Razetime
    Oct 12 '20 at 17:16
  • \$\begingroup\$ @Razetime Nice, thanks! \$\endgroup\$
    – user
    Oct 12 '20 at 17:23
9
\$\begingroup\$

R, 30 bytes

function(l,d)unique(l,l[l!=d])

Try it online!

unique() has the signature unique(x,incomparables = FALSE,...); this sets incomparables to the elements that aren't equal to d, so only d is uniquified.

\$\endgroup\$
8
\$\begingroup\$

Python 2, 50 bytes

l,n=input()
for x in l:
 if~n-x:print x;n^=-(x==n)

Try it online!

Prints output one entry per line.

The idea is to store whether we have already encountered the entry-to-remove n in the sign of n rather than a separate Boolean variable. When we see a list entry that equals n, we negate n. To decide whether to print the current entry x, we check if it equals -n, which checks that it equals the original n and that we've already negated n due to an earlier match. Note that since n and list entries are positive, there's no way to get x==-n before n is negated.

Well, actually, instead of negating n, it's shorter to bit-complement it to ~n, which is -n-1. To do the conditional complementing, we note that we can convert [x,~x][b] to x^-b (as in this tip), using that bitwise xor ^ has x^0==x and x^-1==~x. So, we do n^=-(x==n).

\$\endgroup\$
7
\$\begingroup\$

JavaScript (ES6),  32  30 bytes

Expects (x)(list).

x=>a=>a.filter(v=>v^x||a[a=0])

Try it online!

How?

All values v that are not equal to x are preserved thanks to v^x. The first value that is equal to x is kept as well because a[0] is guaranteed to be a positive integer (except if a is empty, but then we don't enter the .filter() loop to begin with). For the next values that are equal to x, we have a = 0 and a[0] === undefined, so they are rejected. This test doesn't throw an error because Numbers are Objects, so it's legal to access the (non-existent) property '0' of 0.

\$\endgroup\$
6
\$\begingroup\$

Haskell, 43 bytes

a%(b:c)|a==b=b:filter(/=a)c|1<2=b:a%c
_%x=x

Try it online!

Ungolfed:

dedupl v (x:xs)
  | x == v = x : filter (/= v) xs
  | otherwise = x : dedupl v xs
dedupl _ [] = []

Haskell, 40 bytes

This version takes a (negative) predicate for input instead.

f%(b:c)|f b=b:f%c|1<2=b:filter f c
_%x=x

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Simple and elegant! How I didn't thought about that? How I messed up doing complicated stuffs when the solution was so obviously simple? Haskell journey is getting interesting day by day \$\endgroup\$
    – AZTECCO
    Oct 13 '20 at 21:40
5
\$\begingroup\$

C (gcc), 60 bytes

Saved 2 bytes thanks to ceilingcat!!!
Saved 2 bytes thanks to ErikF!!!

t;f(d,l)int*l;{for(t=0;*l;++l)*l==d&&t++||printf("%d ",*l);}

Try it online!

Inputs a number and a pointer to a null terminated array (as there's no way to know the length of an array passed into a function in C) and outputs the filtered array to stdout.

Explanation

f(d,                        // function taking the duplicate number d,  
    l)int*l;{               // a null terminated array of int l  
  for(                      // loop...  
      t=0;                  //   init test flag t to 0, this will mark the  
                            //   1st (if any) occurance of d  
          *l;               // ...over the array elements  
              ++l)          // bumping the array pointer each time  
      *l==d                 // if the array element isn't d...  
           &&t              //   or it's the 1st time seeing d  
               ++           //   unmark t by making it non-zero  
      ||printf("%d ",*l);   // ...then print that element  
}
\$\endgroup\$
2
  • \$\begingroup\$ If you use 0 as a sentinel, you don't need to take a length (61 bytes): Try it online! \$\endgroup\$
    – ErikF
    Oct 12 '20 at 20:40
  • \$\begingroup\$ @ErikF Nice one - thanks! :-) \$\endgroup\$
    – Noodle9
    Oct 12 '20 at 20:53
5
\$\begingroup\$

APL (Dyalog Unicode), 18 15 10 bytes

Thanks to Adám for -8 bytes!!!

∊⊢⊆⍨≠∨<\⍤=

Try it online!

Example inputs: left argument 3, right argument 1 2 3 4 3 4.

= does element-wise not-equal comparison. => 0 0 1 0 1 0
<\ Scans with less-than. This keeps just the first 1, all other places are 0. => 0 0 1 0 0 0
≠∨ does element-wise OR with the mask. => 1 1 1 1 0 1.
⊢⊆ partitions the input based on the vector, including positions with positive integers. => (1 2 3 4) (4) flattens the nested array. => 1 2 3 4 4

\$\endgroup\$
3
  • \$\begingroup\$ -3: ∊⊢⊆⍨≠∨¯1↓1,⌊\⍤≠ Try it online! \$\endgroup\$
    – Adám
    Oct 12 '20 at 10:05
  • \$\begingroup\$ @Adam thanks a lot! \$\endgroup\$
    – ovs
    Oct 12 '20 at 10:27
  • \$\begingroup\$ -5: ¯1↓1,⌊\⍤≠<\⍤= Try it online! \$\endgroup\$
    – Adám
    Oct 14 '20 at 11:12
5
\$\begingroup\$

Jelly,  6  5 bytes

-1 thanks to Sisyphus's suggestion to use in place of W€

Ẇi¦⁹ḟ

A full program accepting the list and the value which prints the Jelly representation of a list with all but the first occurrence of the value removed (empty lists print nothing, lists with one element print that element).

Try it online! Or see the test-suite.

How?

Ẇi¦⁹ḟ - Link: list, A; value V
  ¦   - sparse application...
 i ⁹  - ...to indices: first occurrence of V in A  ([0] if no V found)
W     - ...action: all non-empty sublists (since ¦ zips, the element, z, at any
                                           given index of A will be [z])
    ḟ - filter discard occurrence of V (leaves the [z] as is)
      - implicit print

I thought ḟẹḊ¥¦ would work for 5, but it fails with a divide by zero error with [5,5] and 5.

\$\endgroup\$
2
  • \$\begingroup\$ I think Ẇi¦⁹ḟ works for 5. \$\endgroup\$
    – Sisyphus
    Oct 16 '20 at 4:19
  • \$\begingroup\$ @Sisyphus Nice, it will, since the first n entries of the results of for a length n list are the wrapped elements of that list. \$\endgroup\$ Oct 17 '20 at 15:20
4
\$\begingroup\$

Japt, 10 7 bytes

kȶV©T°

Try it

-3 bytes thanks to caffeine!

kȶV©T°     :Implicit input of array U and integer V
k           :Remove the elements in U that return true
 È          :When passed through the following function
  ¶V        :Is equal to V?
    ©       :Logical AND with
     T°     :Postfix increment T (initially 0)
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 6 bytes

Ê0X.;Ï

Integer as first input, list as second input.

Try it online or verify all test cases.

Explanation:

Ê      # Check for each value in the second (implicit) input-list whether it's NOT equal
       # to the first (implicit) input-integer (1 if NOT equal; 0 if equal)
 0X.;  # Replace the first 0 with a 1
     Ï # And only keep the values in the (implicit) input-list at the truthy (1) indices
       # (after which the result is output implicitly)
\$\endgroup\$
3
\$\begingroup\$

Bash + sed, 49 bytes

sed "s/\b$1\b/_/;s/\b$1\b \?//g;s/_/$1/"<<<${*:2}

Try it online!

Takes the first argument as the duplicate and the rest as the array.

\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog Unicode), 9 bytes (SBCS)

Translation of Galen Ivanov's J solution.

Anonymous tacit infix function, taking number as left argument and list as right argument (though argument order can be switched by changing the s into s).

∊⊢⊆⍨≠∨∘≠⊢

Try it online!

 on the right argument

∘≠ apply nub-sieve (Boolean list with Trues where unique elements occur first), then:

 … element-wise OR that with:

   Boolean list with Trues where elements in the list are different from the number

⊆⍨ corresponding to runs of Trues in that, extract runs in:

 the list

ϵnlist (flatten)

\$\endgroup\$
3
\$\begingroup\$

Wolfram Language (Mathematica), 26 24 bytes

#2/.(a=#)/;a++>#:>Set@$&

Try it online!

The pattern (a=#) to be matched is only evaluated once, at the very start. Then, the condition a++># is only evaluated when the pattern is matched - so a will have been incremented on subsequent matches.

\$\endgroup\$
2
\$\begingroup\$

Red, 46 bytes

func[n b][try[replace/all find/tail b n n[]]b]

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python 3, 62 bytes

f=lambda n,l:l.count(n)>1and f(l.pop(~l[::-1].index(n)),l)or l

Try it online!

This function will recursively pop the last instance of the given value until no more than one instance is present. Then it returns the list.

or, for the same byte count

lambda n,l:[j for i,j in enumerate(l)if j!=n or i==l.index(n)]

Try it online!

This is just a simple filter.

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 8 bytes

ʒÊD¾ms_½

Try it online!

Commented:

ʒ          # filter the first input on ...
 Ê         #   not equal to the second input (n)?
  D        #   duplicate this value
   ¾       #   push the counter variable initially 0
    m      #   power (value != n)**(counter)
           #   this is only 0 if value==n and counter is positive
     s     #   swap to (value != n)
      _    #   negate this
       ½   #   increment the counter variable if this is truthy (value == n)
\$\endgroup\$
2
\$\begingroup\$

Perl 5, 36 bytes

sub{$n=pop;$i=0;grep$n-$_||!$i++,@_}

Try it online!

Pop last input value from @_ into $n. The remaining @_ is the input list. Filter (grep) @_ for the values that either isn't equal to $n ($n-$_ is truthy when $n and current list value $_ is different) or is the first equal to $n since !$i++ is truthy for the first and not for the rest.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ There's no need for the $i=0; part, unless you're using the sub multiple times. Which you shouldn't as this is code golf. \$\endgroup\$
    – Abigail
    Oct 12 '20 at 21:31
2
\$\begingroup\$

J, 15 10 bytes

-5 bytes thanks to xash!

]#~=<:~:@]

Try it online!

My initial solution:

J, 15 bytes

[#~~:+i.@#@[=i.

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ @xash Really nice, thanks! +. could be >. :) \$\endgroup\$ Oct 12 '20 at 13:23
  • 1
    \$\begingroup\$ Ah, with that kind of logic it's 10 bytes :-) \$\endgroup\$
    – xash
    Oct 12 '20 at 13:31
  • \$\begingroup\$ @xash That's great, thanks again! \$\endgroup\$ Oct 12 '20 at 13:35
2
\$\begingroup\$

APL+WIN, 19 bytes

Prompts for vector followed by element to be removed:

((v≠n)+<\v=n←⎕)/v←⎕

Try it online! Thanks to Dyalog Classic

\$\endgroup\$
2
\$\begingroup\$

Husk, 13 12 bytes

F+ṀΓ·:f≠⁰↕≠⁰

Try it online!

user's answer.(-3 bytes, then -1 byte.)

Husk, 16 bytes

J²fI§e←of≠²→↕≠²⁰

Try it online!

Can probably be shortened with Γ.

There may be an extremely short solution with ü as well.user's answer

+2 bytes after supporting numbers not in the list.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ The 5-byter doesn't work on test cases 5 and 6. \$\endgroup\$
    – Zgarb
    Oct 12 '20 at 15:02
  • \$\begingroup\$ @user once arguments are used explicitly, they can't be removed. \$\endgroup\$
    – Razetime
    Oct 12 '20 at 16:43
  • 1
    \$\begingroup\$ @user You don't need to apologize to me for posting your own answer :) \$\endgroup\$
    – Razetime
    Oct 12 '20 at 17:14
2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 42 bytes

a=>b=>a.Where((x,i)=>x!=b|i==a.IndexOf(b))

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ -1 byte with is operator sorcery. \$\endgroup\$ Oct 13 '20 at 13:57
  • \$\begingroup\$ @thedefault. That is very impressive, I would've never gotten there, you don't want to post that yourself? \$\endgroup\$
    – LiefdeWen
    Oct 13 '20 at 14:08
2
\$\begingroup\$

Scala, 62 61 58 bytes

a=>s=>{val(c,d)=s splitAt s.indexOf(a)+1;c++d.filter(a!=)}

Try it online!

  • Thanks to Galen for -1 character
  • Thanks to user for -3 characters
\$\endgroup\$
0
2
\$\begingroup\$

Haskell, 55 bytes

f n=foldl(\a x->if x==n&&x`elem`a then a else a++[x])[]

Try it online!

  • Previous answer was complicated and not so good so I decided to try a more expressive approach inspired by @Caagr98 answer, mine is still longer but I feel better now =)

# Previous 72 bytes
g b n(h:t)
 |h/=n=h:g b n t
 |b>0=g 1n t
 |1>0=h:g 1n t
g b n _=[]
f=g 0

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thank you @user ! Anyway this is a mess, compared to codegolf.stackexchange.com/a/212430/84844 mine is not only longer, it's not elegant and and less readable. I think Caagr98 answer is exactly as expressive as Haskell is meant to be \$\endgroup\$
    – AZTECCO
    Oct 13 '20 at 21:32
2
\$\begingroup\$

PowerShell Core, 50 31 bytes

param($a,$b)$b|?{$_-$a-or!$o++}

-19 bytes thanks to mazzy!

Try it online!

Iterates on the array passed as the second parameter and ignores the duplicate occurrence of the first parameter.

\$\endgroup\$
2
2
\$\begingroup\$

JavaScript (Node.js), 117 113 93 bytes

function x(i,j){var d,e,o=[];for(x in i){e=i[x]==j;!(e&&d)?o.push(i[x]):0;e?d=1:0;}return o;}

Try it online!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ It's a good practice to hose your programs on tio so people can check: Try it online! \$\endgroup\$
    – Razetime
    Oct 19 '20 at 3:44
  • \$\begingroup\$ Thanks, @Razetime \$\endgroup\$ Oct 19 '20 at 3:47
  • 1
    \$\begingroup\$ You can get this to 62 bytes, I think. Probably a lot more with filter and more golfing tricks, but I don't know JS that well \$\endgroup\$
    – user
    Oct 21 '20 at 15:00
1
\$\begingroup\$

Charcoal, 12 bytes

IΦη∨⁻ιθ⁼κ⌕ηι

Try it online! Link is to verbose version of code. Explanation:

  η             Input list
 Φ              Filtered where
     ι          Current element
    ⁻           Subtract (i.e. does not equal)
      θ         Input integer
   ∨            Logical Or
        κ       Current index
       ⁼        Equals
         ⌕      First index of
           ι    Current element in
          η     Input list
I               Cast to string
                Implicitly print

The last character could also be θ of course since the two variables are equal at that point.

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

Python 2, 55 52 bytes

Thanks to xnor for -3 bytes!

Output is newline-separated.

n,l=input()
x=1
for d in l:
 if x|d-n:print d;x*=d-n

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ It looks like it's shorter to just write d-n in place of w. \$\endgroup\$
    – xnor
    Oct 12 '20 at 9:51
  • \$\begingroup\$ @xnor It is indeed. I initially had d!=n and it was the same length, but I forgot to check again. Thanks a lot! \$\endgroup\$
    – ovs
    Oct 12 '20 at 10:00
1
\$\begingroup\$

K (Kona), 21 bytes

{y@&(~x=y)+(!#y)=y?x}

Try it online!

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

Stacked, 28 bytes

[@y:0@b[b\y=:b+@b*¬]"!keep]

Try it online!

Explanation

[@y:0@b[b\y=:b+@b*¬]"!keep]
[                         ]   anonymous function (expects 2 args)
 @y                           save top as y
    0@b                       initialize b = 0
   :   [           ]"!        for each element E in the input array:
        b\                      save the current value of b for later computation
          y= b+@b               b = max(b, y == E)
        b y=:    *¬             not both (old b) and (y == E) are true
                                for y != E, and for the first y == E, this is 1, else 0
                              this generates a mask of 1s and 0s
                      keep    keep only the elements in the input which correspond to a 1
      

Other Solutions

51 bytes: [@y()@z{e:[z e push][z y∈¬*]$!e y=ifelse}map@.z]

41 bytes: [@y::inits[:y index\#'1-=]map\y neq+keep]

36 bytes: [@y:0@b[b\:y=b\max@b y=*¬]map keep]

33 bytes: [@y:0@b[b\:y=b+@b y=*¬]map keep]

\$\endgroup\$
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\$

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.