In: Computer Science
Finding duplicate values of an array of integer values.
Example 1
Input: arr[] = {5, 2, 7, 7, 5}, N = 5
Output:
Duplicate Element: 5
Duplicate Element: 7
Example 2
Input: arr[] = {1, 2, 5, 5, 6, 6, 7, 2}, N = 8
Output:
Duplicate Element: 2
Duplicate Element: 5
Duplicate Element: 6
CODE::
class Main {
public static void FindDuplicate(int[] arr){
// write your code to find duplicates here and print those
values
}
public static void main(String[] args) {
// take array size as input from keyboard here
// take the array elements as input from keyboard here
}
}
import java.util.Scanner; class Main { public static void FindDuplicate(int[] arr){ int count; for(int i = 0;i<arr.length;i++){ count=0; for(int j = 0;j<i;j++){ if(arr[i]==arr[j]){ count++; } } if(count==0) { count = 0; for (int j = i+1; j < arr.length; j++) { if (arr[i] == arr[j]) { count++; } } if(count==1){ System.out.println("Duplicate Element: "+arr[i]); } } } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); // take array size as input from keyboard here int size = scan.nextInt(); int arr[] = new int[size]; // take the array elements as input from keyboard here for(int i = 0;i<size;i++){ arr[i] = scan.nextInt(); } FindDuplicate(arr); } }