rmduplicate.java
33 linesjava
DOWNLOAD
1// Aim: Program to perform Array operations (Remove Duplicate).
2import java.util.*;
3public class rmduplicate {
4    public static void main(String[] args) {
5        Scanner sc = new Scanner(System.in);
6        int arr[] = new int[30];
7        System.out.print("Enter the number of elements: ");
8        int n = sc.nextInt();
9        System.out.print("Enter the elements: ");
10        for(int i=0; i<n; i++) {
11            arr[i] = sc.nextInt();
12        }
13        System.out.println("Original array: ");
14        for(int i=0; i<n; i++) {
15            System.out.print(arr[i] + " ");
16        }
17        for(int i=0; i<n; i++) {
18            for(int j=i+1; j<n; j++) {
19                if(arr[i] == arr[j]) {
20                    for(int k=j; k<n-1; k++) {
21                        arr[k] = arr[k+1];
22                    }
23                    n--;
24                    j--;
25                }
26            }
27        }
28        System.out.println("Array after removing duplicates: ");
29        for(int i=0; i<n; i++) {
30            System.out.print(arr[i] + " ");
31        }
32    }
33}