# Remove excessive occurrences

Given an array $$\A\$$ of positive integers between $$\1\$$ and $$\9\$$ inclusive and a positive integer $$\n\$$, remove all integers in $$\A\$$ which appear more than $$\n\$$ times.

For example:

             [1,2,3,2,3,2,4,1,2], n = 2
Occurrence:   1 1 1 2 2 3 1 2 4
Keep?         1 1 1 1 1 0 1 1 0
Kept values: [1,2,3,2,3,  4,1  ]


so the output is [1,2,3,2,3,4,1]

You can assume that:

• At least one element will always appear more than $$\n\$$ times
• At least one element will appear fewer than $$\n\$$ times
• $$\A\$$ will have a minimum of 2 distinct elements
• $$\n\$$ and the length of $$\A\$$ are within the integer bounds of your language.

You may take input and output in any convenient method. As the elements of $$\A\$$ are digits, you may also take input as an integer, or a single string, if that suits you.

This is so the shortest code in bytes wins

## Test cases

A, n -> out
[7,3,3,9], 1 -> [7,3,9]
[4,1,7,7,4,2,1], 1 -> [4,1,7,2]
[6,9,7,6,7,9,9], 2 -> [6,9,7,6,7,9]
[6,5,3,9,4,9,6,9], 2 -> [6,5,3,9,4,9,6]
[4,8,9,7,3,1,7,5,4,6], 1 -> [4,8,9,7,3,1,5,6]
[5,6,7,8,1,7,3,3,6,4], 1 -> [5,6,7,8,1,3,4]
[2,3,8,1,6,4,8,2,8,3], 2 -> [2,3,8,1,6,4,8,2,3]
[4,1,4,4,2,4,4,5,7,1,3], 1 -> [4,1,2,5,7,3]
[1,5,5,2,5,1,4,1,6,7,7,6,9,8,8,1,7,8,1,3,1,1,7,4], 3 -> [1,5,5,2,5,1,4,1,6,7,7,6,9,8,8,7,8,3,4]
[8,8,6,2,9,6,4,7,1,2,9,5,5,6,6,2,5,8,9,1,2,8,4,4,7,2,1,1], 3 -> [8,8,6,2,9,6,4,7,1,2,9,5,5,6,2,5,8,9,1,4,4,7,1]
[8,8,4,9,7,1,2,9,8,8,5,2,2,9,1,7,4,6,6,8,6,6,1,6,1,9,2,8,5,1,2,6,1,7,6,4,7,9], 4 -> [8,8,4,9,7,1,2,9,8,8,5,2,2,9,1,7,4,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9,6,1,5,4,1,7,2,4,3,8,5,1,9,5,9,1,7,4,7,3,1,2,1,3,8,7,9,2,5,1,6,2,4,5,9,1,5,5,5,2,6,3,7,2,2,5,7,6,8,3,7,5,9,3,3,9], 7 -> [3,4,1,1,2,4,4,8,4,9,1,1,7,7,6,8,6,2,9,4,2,9,9,7,5,7,6,5,3,7,6,6,1,9,1,5,2,4,9,6,1,5,4,7,2,3,8,5,9,5,7,3,2,3,8,2,5,6,3,8,3]


Test case generator. Run with no arguments to have a max length of 100 for $$\A\$$. For a different max length (no larger than $$\891\$$), run with that as an argument.

• Brownie points for beating my 8 byte Jelly answer Commented Feb 24, 2021 at 0:11
• @manatwork Yeah, you can take input in either order Commented Feb 24, 2021 at 0:19
• I like how well thought and friendly this challenge is: no corner cases with empty arrays; values are single-digit positive integers so more esolangs can participate Commented Feb 24, 2021 at 1:05
• @LuisMendo I like "Remove excessive repetitions", changed to that :) Commented Feb 24, 2021 at 1:14
• Correction: brownie points for beating 6 bytes in Jelly Commented Feb 24, 2021 at 1:18

$t=@{}$a|?{++$t.$_-le$b}  -1 byte thanks to mazzy! Try it online! • Commented Feb 24, 2021 at 8:04 • Oh, learning new tricks everyday, thanks! Commented Feb 24, 2021 at 19:27 # R, 48 bytes function(x,n)x[diag(diffinv(outer(x,x,"=="))<n)]  Try it online! Test harness taken from Dominic's answer. Closely related to Luis Mendo's answer, though implementing it in MATL is longer. Rather than zeroing out the lower triangle of the matrix and computing the column sums, this calculates the cumulative sum of each column, then takes the diagonal of the resulting matrix as the filtering criterion -- for some element $$\a_i\$$ this tells the number of occurrences of $$\a_i\$$ before index $$\i\$$. So for instance, using [6,9,7,6,7,9,9]: Pairwise equality check: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 1 0 0 1 0 0 0 [2,] 0 1 0 0 0 1 1 [3,] 0 0 1 0 1 0 0 [4,] 1 0 0 1 0 0 0 [5,] 0 0 1 0 1 0 0 [6,] 0 1 0 0 0 1 1 [7,] 0 1 0 0 0 1 1 Column-wise cumulative sum (with an initial condition of 0): [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 0 0 0 0 0 0 0 [2,] 1 0 0 1 0 0 0 [3,] 1 1 0 1 0 1 1 [4,] 1 1 1 1 1 1 1 [5,] 2 1 1 2 1 1 1 [6,] 2 1 2 2 2 1 1 [7,] 2 2 2 2 2 2 2 [8,] 2 3 2 2 2 3 3 The diagonal: [1] 0 0 0 1 1 1 2  # K (ngn/k), 18 16 bytes {x@&~y<+/'x=,\x}  Try it online! Adapted from @rak1507's message in the APL Orchard. Takes the list as x and the number of occurrences as y. • +/'x=,\x get the running totals of the counts of each number in the input • &~y< get indices of the first y instances of each value • x@ index back into the input # APL (Dyalog Unicode), 23 19 bytes Saved a couple bytes by looking at Bubbler how negated the mask in their answer. {⍺/⍨(⊢∨(≠⍺×~))⍣⍵⊢0}  Don't try it online! because TIO has an older version of Dyalog APL without Unique Mask. Takes the array on the left and n on the right. This takes advantage of monadic ≠, which gives a mask to obtain unique elements. While ≠A lets you keep unique elements in A, the mask 1-≠A inverts it and lets you keep only repeated elements in A. Then A×1-≠A replaces unique elements with zeroes (this is safe because as the question states, A only contains numbers 1-9). The mask ≠A×1-≠A gives us only unique elements from this new array, but since we replaced unique elements, it lets us keep duplicates (and the first element, but that is always kept no matter what anyway). (≠A)∨≠A×1-≠A now gives us a mask for unique elements and duplicates, but not triplicates, etc. By repeating this process n times, we get a mask for keeping only n-plicates, and then we just apply that mask to A to get the result. # Jelly, 5 bytes œ&Qx¥  Try it online! Accidental translation of Razetime's Stax answer based on his comment. œ& Multiset intersection of A with Q ¥ the unique elements of A x repeated n times.  # Jelly, 7 bytes xċṪ$Ƥ<¥


Try it online!

I was going to hold off on posting until I tied caird's 6 bytes, but I feel like I'm so far off the mark that it's worth documenting anyhow.

x     ¥    Repeat each element of A by the corresponding elements of:
Ƥ      for each prefix of A,
ċ         how many times it contains
Ṫ$its last element (which is then not considered). < Is each count less than n?  • In hindsight, this is an obvious improvement on my original 8 byte version, so I’ve definitely +1’ed. As a clue tho, my 6 byte version has a completely different approach Commented Feb 24, 2021 at 2:12 • you might be able to use the same approach I did with œ&. Commented Feb 24, 2021 at 2:12 # Java, 157 bytes l->n->java.util.stream.IntStream.range(0,l.length).filter(i->java.util.stream.IntStream.range(0,i+1).filter(j->l[j]==l[i]).count()<=n).map(i->l[i]).toArray()  Takes a int[] and the integer n as input and returns a int[]. Try it online! • Welcome to Code Golf! Nice first answer. Commented Feb 24, 2021 at 19:10 • @RedwolfPrograms Thank you. Commented Feb 24, 2021 at 19:11 • Welcome to Code Golf! It's recommended to link to an online playground. I'd suggest using TIO; here's a link. You can use currying to save a byte, and you can omit the semicolon at the end. – user Commented Feb 24, 2021 at 20:24 • @user Thanks for the help. Commented Feb 24, 2021 at 20:34 • @iota Oops, you're right. It's unordered too, so this won't work either :( – user Commented Feb 24, 2021 at 20:37 # R, 62 bytes function(x,n)x[sapply(seq(x),function(i)sum(x[1:i]==x[i]))<=n]  Try it online! ...although a port of Luis Mendo's answer is shorter at 59 58 bytes (Thanks to Giuseppe) • 58 bytes on the port. Commented Feb 24, 2021 at 15:44 • @Giuseppe - Thanks - although your own diag version is much better... Commented Feb 25, 2021 at 9:37 # T-SQL, 114 bytes SELECT C FROM (SELECT B,C,row_number() OVER (PARTITION BY C ORDER BY B) AS D FROM @A) E WHERE D <= @n ORDER BY B; Example: DECLARE @A TABLE(B INT IDENTITY(1,1), C INT); DECLARE @n INT; INSERT INTO @A SELECT * FROM (VALUES(1),(2),(3),(2),(3),(2),(4),(1),(2)) AS A(C); SET @n = 2; SELECT C --4.Finally select the filtered rows FROM (SELECT B ,C ,row_number() OVER (PARTITION BY C ORDER BY B) AS D --1.Create an ordinal for each repetition that increments in ascending order of B FROM @A ) E WHERE D <= @n --2.Filter on the windowed function in subquery, removing rows with more than n repetitions ORDER BY B; --3. Preserve original order of table variable @A (Can't order by in subquery)  Returns: C 1 1 2 2 3 3 4 2 5 3 6 4 7 1 Only way I could simulate an 'array' was by making a table with an identity on insert to store the insertion order. • Welcome to Code Golf, nice first answer! Commented Feb 25, 2021 at 4:49 • @Redwolf Programs Thanks. I'm gonna try and sneak a T-SQL answer in wherever I can :) Commented Feb 28, 2021 at 4:15 # Retina 0.8.2, 52 bytes \d+$
$* (.)(?=.*,(.)+)(?<=(?(2)$)(?<-2>\1.*)+.)|,.+



Try it online! Link includes test cases. Takes A as a string of digits and n as a positive integer separated by a comma. Explanation:

\d+*


Convert n to unary.

(.)(?=.*,(.)+)(?<=(?(2)$)(?<-2>\1.*)+.)|,.+  Delete any digit of A that is preceded by at least n identical digits and also delete n from the result. # Perl 5-al, 28 bytes $l=<>;map$k{$_}++<$l&&say,@F  Try it online! # Ruby, 38 bytes ->a,n{w=[0]*10;a.reject{|x|n<w[x]+=1}}  Try it online! # C# (Visual C# Interactive Compiler), 56 bytes (a,n)=>{var b=new int[10];return a.Where(c=>b[c]++<n);}  Try it online! Or 66 bytes if output should explicitly be an array vs an enumerable: (a,n)=>{var b=new int[10];return a.Where(c=>b[c]++<n).ToArray();}  Try it online! • The first one is fine, if you wanna display it in TIO when using the interactive compiler, just use the Print function Commented Feb 25, 2021 at 13:01 • 55 bytes Commented Feb 25, 2021 at 13:05 # Japt, 8 bytes fVÆâÃc 2  Very similar to the Jelly, Stax, and Husk answers. Explanation:  # Implicitly store the input array as U and the input number as V VÆ # Create an array of length V âÃ # Each item in the array is the unique elements of U c # Flatten that array f # Only keep elements of U that are in that array... 2 # ... up to the number of times it appears in that array  # Charcoal, 11 bytes ＮθΦη‹№…ηκιθ  Try it online! Link is to verbose version of code. Takes input n as an integer and A as a string of digits. Explanation: Ｎθ Input n as an integer η Input A Φ Filtered where № Count of ι Current character in …ηκ Prefix so far ‹ Is less than θ Input n Implicitly print  # Julia 1.0, 46 43 bytes l$n=(j=0;!i=sum(l[1:(j+=1)].==i)<=n;l[.!l])


Try it online!

# C (clang), 62 bytes

f(*l,n){for(int m[10]={0};*l;++l)m[*l]++<n&&printf("%d ",*l);}


Try it online!

• Expects a 0 terminated array as input

If tacking a 0 filled array as an additional input is valid:

# C (clang), 58 bytes

f(*l,n,*o){for(int m[10]={0};*l;++l)m[*l]++<n&&(*o++=*l);}


Try it online!

• -6 Bytes for the seconds solution.
– xibu
Commented Mar 2, 2021 at 23:27

f n|let a?x|sum[1|y<-a,y==x]<n=a++[x]|1>0=a=foldl(?)[]


Try it online!

• I was on the right track, one big step behind @xnor answer which cost me 10 bytes!

# Clojure, 76 bytes

#(for[[i v](map vector(range)%1):when(<(count(filter #{v}(take i %1)))%2)]v)


Try it online!

# SNOBOL4 (CSNOBOL4), 87 bytes

	N =INPUT
T =TABLE()
R	I =INPUT	:F(END)
T<I> =T<I> + 1
OUTPUT =I LE(T<I>,N)	:(R)
END


Try it online!

Takes input as N, then A, each element separated by newlines.

	N =INPUT			;* Read in N, number to keep
T =TABLE()			;* create an empty table
R	I =INPUT	:F(END)		;* read in the next input; if none exists, exit
T<I> =T<I> + 1			;* increment the value at index I (initially 0)
OUTPUT =I LE(T<I>,N)	:(R)	;* if T<I> is LEQ to N, then output I. Goto R.
END