Given an array of integers, find all the elements that are duplicated.
Example:
Input: [1, 2, 3, 4, 3, 2, 5]
Output: [2, 3]
Hint:
You can use a HashSet to track elements you have already seen. If an element is already in the set, it's a duplicate. For preserving order, use LinkedHashSet to store the duplicates.
Java Code Using HashSet
import java.util.Arrays; import java.util.HashSet; import java.util.List; public class Test { public static void main(String[] args) { Listli = Arrays.asList(1, 2, 3, 4, 3, 2, 5); HashSet hs = new HashSet (); for (int i = 0; i
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3