coding question - find the duplicate elements present in a given array
Find duplicate elements in an array
Iterate through the array and compare each element with all other elements
If a match is found, add it to a separate array
Return the array of duplicate elements
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter size of array: ");
int size = scanner.nextInt();
int[] arr = new int[size];
System.out.print("Enter array elements: ");
for(int i=0; i<size; i++) {
arr[i] = scanner.nextInt();
}
Set<Integer> set = new HashSet<>();
Set<Integer> duplicates = new HashSet<>();
for (int i : arr) {
if (set.contains(i)) {
duplicates.add(i);
} else {
set.add(i);
}
}
System.out.println("Duplicate elements: " + duplicates);
}
}
Top Accenture Application Development Associate interview questions & answers
Popular interview questions of Application Development Associate
Top HR questions asked in Accenture Application Development Associate
Reviews
Interviews
Salaries
Users/Month