Problem 59801. Limiting Element Occurrences
You are given a list 'v' of numbers. This list may contain repeated numbers. Your objective is to create a new list that maintains the same elements in the same order as the original list. However, if a number appears more than 'k times in the original list, only the first 'k' occurences of that number will be included in the new list. Any additional instances of that number beyond the k-th occurrence will be discarded. Note that, if k equals 0, the resulting list would be empty.
Example: if v = [ 1, 3, 5, 3, 1, 3, 5, 3, 1 ] and k = 2, the function should output [ 1, 3, 5, 3, 1, 5 ].
Solution Stats
Problem Comments
-
3 Comments
Christian Schröder
on 1 Apr 2024
A tip: it's best to use isempty() instead of comparing to [], since [] is specifically 0-by-0 and not equal to e.g. a 0-by-1 array.
minnolina
on 13 May 2024
Christian, you're right, I've made the change. Thanks for the tip!
Christian Schröder
on 13 May 2024
Thanks, minnolina!
Solution Comments
Show commentsProblem Recent Solvers11
Suggested Problems
-
Return the 3n+1 sequence for n
8374 Solvers
-
Rotate input square matrix 90 degrees CCW without rot90
641 Solvers
-
Replace Nonzero Numbers with 1
411 Solvers
-
116 Solvers
-
For a rectangle, if x is the length and 2x is width. Then find out x from the area of the rectangle?
97 Solvers
More from this Author53
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!