Approach

  Blog    |     January 27, 2026

To solve this problem, we need to identify the real products from a given list by excluding any products that are marked as fake. The solution involves efficiently filtering out the fake products from the list of all products.

  1. Problem Analysis: The task is to process two lists: one containing all products and another containing fake products. The goal is to return a list of products that are not present in the fake products list.
  2. Intuition: By converting the list of fake products into a set, we can leverage the O(1) average time complexity for membership checks. This allows us to efficiently filter the products list in linear time.
  3. Algorithm Selection:
    • Convert the fake_products list into a set for O(1) lookups.
    • Iterate through each product in the products list and include it in the result only if it is not found in the fake products set.
  4. Complexity Analysis:
    • Time Complexity: O(n + m), where n is the number of products and m is the number of fake products. Converting the fake products list to a set takes O(m) time, and iterating through the products list takes O(n) time.
    • Space Complexity: O(m) for storing the set of fake products. The result list uses O(n) space in the worst case (if no products are fake).

Solution Code

def find_real_products(products, fake_products):
    fake_set = set(fake_products)
    real_products = [product for product in products if product not in fake_set]
    return real_products

Explanation

  1. Conversion to Set: The list of fake products is converted into a set (fake_set) to allow O(1) average time complexity for checking if a product is fake.
  2. List Comprehension: Using a list comprehension, each product in the products list is checked against fake_set. If the product is not in the set, it is included in the result list (real_products).
  3. Result Compilation: The resulting list real_products contains all products from the original list that are not marked as fake, effectively filtering out the fake products efficiently.

This approach ensures optimal performance and correctness by leveraging set operations for quick lookups and list comprehensions for concise and readable code.


Request an On-site Audit / Inquiry

SSL Secured Inquiry