User Story
Write a code that efficiently removes duplicate integers from the given list.
List<Integer> codes = new List<Integer>{12, 5, 7, 12, 15, 15, 5, 7, 9};
Solution
Apex Method:
Create a Set to store unique elements:
List<Integer> codes = new List<Integer>{12, 5, 7, 12, 15, 15, 5, 7, 9};
System.debug('Original List: ' + codes);
Set<Integer> uniqueSet = new Set<Integer>(codes);
Clear the original list and add elements from the Set
List<Integer> codes = new List<Integer>{12, 5, 7, 12, 15, 15, 5, 7, 9};
System.debug('Original List: ' + codes);
Set<Integer> uniqueSet = new Set<Integer>(codes);
codes.clear();
codes.addAll(uniqueSet);
System.debug('List after removing duplicates: ' + codes);
Debug
Original List: (12, 5, 7, 12, 15, 15, 5, 7, 9)
List after removing duplicates: (12, 5, 7, 15, 9)
Video
Video does not exists.