Skip to content
CodeKruPro

Fewest distinct values after removing k items

Coding problemMediumAsked in: Fivetran

Given a list of IDs and a number k, remove exactly k items from the list so that as few distinct IDs as possible are left. Return how many distinct IDs remain.

k is never larger than the length of the list.

Examples

Example 1
Input: ids = [5,5,4], k = 1
Output: 1
Remove the 4; only 5 is left.
Example 2
Input: ids = [4,3,1,1,3,3,2], k = 3
Output: 2
Remove 4 and 2 (one item each) and one 1; 1 and 3 remain.
Example 3
Input: ids = [1,2,3], k = 3
Output: 0

Constraints

  • The list holds between 0 and 100,000 IDs
  • Each ID is between -1,000,000,000 and 1,000,000,000
  • k is between 0 and the length of the list

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in