- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to find number of items left after selling n items in python

Suppose we have a list of numbers called items and another value n. A salesman has items in a bag with random IDs. The salesman can delete as many as n items from the bag. We have to find the minimum number of different IDs in the bag after n removals.

So, if the input is like items = [2, 2, 6, 6] n = 2, then the output will be 1 as we he can sell two items with ID 2 or ID 6, then only items with single target will be there.

To solve this, we will follow these steps:

- c := frequency of each element present in items
- ans := size of c
- freq := sort the list of all frequencies in c
- i := 0
- while i < size of freq, do
- if freq[i] <= n, then
- n := n - freq[i]
- ans := ans - 1

- otherwise,
- return ans

- i := i + 1

- if freq[i] <= n, then
- return 0

Let us see the following implementation to get better understanding:

## Example

from collections import Counter class Solution: def solve(self, items, n): c = Counter(items) ans = len(c) freq = sorted(c.values()) i = 0 while i < len(freq): if freq[i] <= n: n -= freq[i] ans -= 1 else: return ans i += 1 return 0 ob = Solution() items = [2, 2, 6, 6] n = 2 print(ob.solve(items, n))

## Input

[2, 2, 6, 6], 2

## Output

1

- Related Articles
- Program to find largest product of three unique items in Python
- Python program to find the sum of all items in a dictionary
- Program to find maximum number of K-sized groups with distinct type items are possible in Python
- Python Program to remove items from set
- Program to find remainder after dividing n number of 1s by m in Python
- Python Program to remove items from the set
- Python Program to Multiply All the Items in a Dictionary
- Program to find maximum profit after cutting rods and selling same length rods in Python
- Program to count items matching a rule using Python
- How to Find The Largest Or Smallest Items in Python?
- Program to find maximize score after n operations in Python
- Program to find maximum profit after buying and selling stocks at most two times in python
- Program to update list items by their absolute values in Python
- Program to count good meals with exactly two items in Python
- How to find Profit or loss using Python when CP of N items is equal to SP of M

Advertisements