Given two lists of integers, return a list containing elements that are in the first list but NOT in the second list. Maintain the order from the first list. Duplicates in the first list should appear once in the result if they are not in the second list.
Examples:
Input: list1 = [1, 2, 3, 4, 5], list2 = [3, 4, 5, 6, 7]
Output: [1, 2]
Explanation: Elements 1 and 2 are in list1 but not in list2
Input: list1 = [1, 1, 2, 2], list2 = [1]
Output: [2]
Explanation: Element 1 is excluded, element 2 appears once in result
Input: list1 = [5, 10, 15], list2 = []
Output: [5, 10, 15]
Explanation: Empty list2 means all elements from list1 are included
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay September 2026 (Sep 1–15)Sponsored by FLXBL
Contest runs September 1 - 15. Complete challenges to climb the leaderboard!
1st & 2nd: Salesforce certification exam voucher — see all prizes
Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.