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!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow 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 Jan 2026
Contest runs January 1 - 31. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.