| Bytes | Lang | Time | Link |
|---|---|---|---|
| 050 | AWK | 241111T211231Z | xrs |
| 023 | Wolfram Language Mathematica | 201012T174917Z | att |
| 058 | jq | 240205T065953Z | peak |
| 006 | Uiua SBCS | 240204T085402Z | chunes |
| 093 | JavaScript Node.js | 201019T032844Z | TheCoder |
| 012 | K ngn/k | 201216T202438Z | coltim |
| 072 | Google Sheets | 201216T184844Z | General |
| 031 | PowerShell Core | 201215T060619Z | Julian |
| 004 | Husk | 201012T170218Z | user |
| 049 | TSQL | 201015T124001Z | t-clause |
| 063 | PHP 63 Bytes | 201017T230520Z | Zsolt Sz |
| 005 | Jelly | 201012T114304Z | Jonathan |
| 010 | APL Dyalog Unicode | 201012T093640Z | ovs |
| 009 | APL Dyalog Unicode | 201014T112101Z | Adá |
| 055 | Haskell | 201012T214849Z | AZTECCO |
| 028 | Stacked | 201013T163811Z | Conor O& |
| 049 | Bash + sed | 201012T184107Z | Noodle9 |
| 058 | Scala | 201013T081131Z | Tomer Sh |
| 042 | C# Visual C# Interactive Compiler | 201013T075427Z | LiefdeWe |
| 043 | Haskell | 201012T235544Z | Kyuuhach |
| 060 | C gcc | 201012T143655Z | Noodle9 |
| 012 | Husk | 201012T092241Z | Razetime |
| 030 | R | 201012T122758Z | Giuseppe |
| 019 | APL+WIN | 201012T133031Z | Graham |
| 010 | J | 201012T103005Z | Galen Iv |
| 021 | K Kona | 201012T130904Z | Galen Iv |
| 006 | 05AB1E | 201012T124414Z | Kevin Cr |
| 050 | Wolfram Language Mathematica | 201012T122104Z | ZaMoC |
| 036 | Perl 5 | 201012T104237Z | Kjetil S |
| 007 | Japt | 201012T091318Z | Shaggy |
| 008 | 05AB1E | 201012T101627Z | ovs |
| 050 | Python 2 | 201012T092620Z | xnor |
| 052 | Python 2 | 201012T084142Z | ovs |
| 012 | Charcoal | 201012T095650Z | Neil |
| 030 | JavaScript ES6 | 201012T091721Z | Arnauld |
| 062 | Python 3 | 201012T082722Z | Jitse |
| 046 | Red | 201012T091944Z | Galen 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&
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
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;}
K (ngn/k), 12 bytes
{y_/|1_&x=y}
1_&x=yidentify indices of duplicates, ignoring the firsty_/|drop values at the specified indices, in reverse order
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 rangeD. - 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.
PowerShell Core, 50 31 bytes
param($a,$b)$b|?{$_-$a-or!$o++}
-19 bytes thanks to mazzy!
Iterates on the array passed as the second parameter and ignores the duplicate occurrence of the first parameter.
Husk, 5 4 bytes
üoEė
- 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?
T-SQL, 49 bytes
Input is an integer and a temporary table
SELECT v FROM # GROUP BY v,i*(v-@)ORDER BY min(i)
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!!!
∊⊢⊆⍨≠∨<\⍤=
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).
∊⊢⊆⍨≠∨∘≠⊢
⊢ 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])[]
- 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
Stacked, 28 bytes
[@y:0@b[b\y=:b+@b*¬]"!keep]
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}
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!=)}
C# (Visual C# Interactive Compiler), 42 bytes
a=>b=>a.Where((x,i)=>x!=b|i==a.IndexOf(b))
Haskell, 43 bytes
a%(b:c)|a==b=b:filter(/=a)c|1<2=b:a%c
_%x=x
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
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);}
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≠⁰↕≠⁰
user's answer.(-3 bytes, then -1 byte.)
Husk, 16 bytes
J²fI§e←of≠²→↕≠²⁰
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])
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←⎕
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~#&
Perl 5, 36 bytes
sub{$n=pop;$i=0;grep$n-$_||!$i++,@_}
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°
-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_½
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)
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
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])
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
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)]
This is just a simple filter.