g | x | w | all
Bytes Lang Time Link
050AWK241111T211231Zxrs
023Wolfram Language Mathematica201012T174917Zatt
058jq240205T065953Zpeak
006Uiua SBCS240204T085402Zchunes
093JavaScript Node.js201019T032844ZTheCoder
012K ngn/k201216T202438Zcoltim
072Google Sheets201216T184844ZGeneral
031PowerShell Core201215T060619ZJulian
004Husk201012T170218Zuser
049TSQL201015T124001Zt-clause
063PHP 63 Bytes201017T230520ZZsolt Sz
005Jelly201012T114304ZJonathan
010APL Dyalog Unicode201012T093640Zovs
009APL Dyalog Unicode201014T112101ZAdá
055Haskell201012T214849ZAZTECCO
028Stacked201013T163811ZConor O&
049Bash + sed201012T184107ZNoodle9
058Scala201013T081131ZTomer Sh
042C# Visual C# Interactive Compiler201013T075427ZLiefdeWe
043Haskell201012T235544ZKyuuhach
060C gcc201012T143655ZNoodle9
012Husk201012T092241ZRazetime
030R201012T122758ZGiuseppe
019APL+WIN201012T133031ZGraham
010J201012T103005ZGalen Iv
021K Kona201012T130904ZGalen Iv
00605AB1E201012T124414ZKevin Cr
050Wolfram Language Mathematica201012T122104ZZaMoC
036Perl 5201012T104237ZKjetil S
007Japt201012T091318ZShaggy
00805AB1E201012T101627Zovs
050Python 2201012T092620Zxnor
052Python 2201012T084142Zovs
012Charcoal201012T095650ZNeil
030JavaScript ES6201012T091721ZArnauld
062Python 3201012T082722ZJitse
046Red201012T091944ZGalen Iv

AWK, 50 bytes

{for(i=1;i++<NF;)s=s ($i~$1&&0!~x++?"":$i" ")}$0=s
awk -vFPAT="[0-9]+" ' {for(i=1;i++<NF;)s=s ($i~$1&&0!~x++?"":$i" ")}$0=s' <<< "5 [1,2,4,5,5,5,3,2]"


{for(i=1;i++<NF;) # iterate array
s=s               # append string
($i~$1            # catch match
&&0!~x++?         # have we seen it before
"":$i" ")         # string to append
}$0=s             # set output

Wolfram Language (Mathematica), 26 24 23 bytes

#2/.(a=#):>a~##&~Set@a&

Try it online!

The pattern (a=#) to be matched is only evaluated once, at the very start. a~##&~Set@a yields # on the first match, but changes a to Sequence[] as a side effect. As a result, on subsequent matches, the expression will yield Sequence[].

#2                      input
  /.   #                find value to remove:
    (a= ):>a~##&~Set@a    (first match) don't change
         :>a~##&~    a    (later match) delete

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

Uiua SBCS, 6 bytes

▽↥⊃◰≠,

Try it!

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!

K (ngn/k), 12 bytes

{y_/|1_&x=y}

Try it online!

Google Sheets, 72

+3 total for named ranges.

How

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.

Husk, 5 4 bytes

üoEė

Try it online!

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?

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

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...)

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.

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

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)

Haskell, 55 bytes

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

Try it online!


# 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!

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]

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.

Scala, 62 61 58 bytes

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

Try it online!

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

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

Try it online!

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!

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  
}

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.

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.

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

J, 15 10 bytes

-5 bytes thanks to xash!

]#~=<:~:@]

Try it online!

My initial solution:

J, 15 bytes

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

Try it online!

K (Kona), 21 bytes

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

Try it online!

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)

Wolfram Language (Mathematica), 50 bytes

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

Try it online!

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.

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)

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)

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).

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!

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.

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.

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.

Red, 46 bytes

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

Try it online!