Find Common Items in Two Customer Wishlists
At Amazon, understanding customer preferences is key to improving the shopping experience. One way we do this is by analyzing customer wishlists. Imagine a scenario where we want to identify items that are popular across different customer segments or find potential bundle opportunities. This problem focuses on a core part of that analysis: finding common items between two distinct customer wishlists.
Your task is to implement a function findCommonItems that takes two arrays of item IDs, representing two customer wishlists. The function should return a new array containing all item IDs that are present in both wishlists. The resulting array of common items must be:
wishlist1: An array of numbers. Each number represents an item ID that a customer has added to their first wishlist.wishlist2: An array of numbers. Each number represents an item ID that a customer has added to their second wishlist.numbers. This array should contain all item IDs that are present in both wishlist1 and wishlist2. The output array must be sorted in ascending order and contain only unique item IDs.wishlist1 or wishlist2 is empty, the function should return an empty array, as there can be no common items.[1, 5, 5, 10]). Your output should still contain only unique common items.Examples
Example 1
Input:
wishlist1 = [1, 5, 2, 8, 5], wishlist2 = [5, 2, 9, 10, 5]
Output:
[2,5]
Note:
Items 2 and 5 are present in both wishlists. Even though 5 appears multiple times, it's listed once. The result is sorted.
Example 2
Input:
wishlist1 = [10, 20, 30], wishlist2 = [15, 25, 35]
Output:
[]
Note:
There are no common items between the two wishlists.
Example 3
Input:
wishlist1 = [1, 2, 2, 3, 4], wishlist2 = [2, 2, 4, 5, 6]
Output:
[2,4]
Note:
Items 2 and 4 are common. Despite multiple occurrences of 2 in both, it appears only once in the output. The result is sorted.
Constraints
0 <= wishlist1.length, wishlist2.length <= 10^41 <= item_id <= 10^9item_id values are integers.Loading editor...