Approach

  Blog    |     February 06, 2026

To solve this problem, we need to filter out fake products from a given list of products. The solution involves efficiently removing all products that appear in the fake products list. Here's the step-by-step approach:

  1. Convert Fake Products to a Set: By converting the list of fake products into a set, we achieve O(1) average time complexity for membership checks. This optimization is crucial for handling large lists efficiently.
  2. Filter Real Products: Iterate through each product in the original list and include it in the result list only if it is not present in the fake products set.
  3. Return the Result: The filtered list contains only the real products, maintaining their original order.

Solution Code

def filter_real_products(products, fakes):
    fake_set = set(fakes)
    return [product for product in products if product not in fake_set]

Explanation

  1. Set Conversion: The list of fake products (fakes) is converted into a set (fake_set). This allows for O(1) average time complexity when checking if a product is fake.
  2. List Comprehension: The list comprehension iterates over each product in the products list. For each product, it checks if the product is not in fake_set. If it is not, the product is included in the result list.
  3. Efficiency: The overall time complexity is O(n + m), where n is the number of products and m is the number of fake products. The space complexity is O(m) for storing the fake products set. This approach efficiently handles large datasets by leveraging set operations for quick lookups.

This solution ensures that only real products are returned in their original order, providing an optimal and clear method to filter out fake products.


Request an On-site Audit / Inquiry

SSL Secured Inquiry