-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRemoveDuplicateElementsFromArray.java
52 lines (52 loc) · 1.3 KB
/
RemoveDuplicateElementsFromArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// WAP to delete the duplicate elements from the array.
import java.util.Scanner;
public class RemoveDuplicateElementsFromArray{
public static int[] readArr()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Size of Array : ");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter the "+n+" Elements of Array : ");
for(int i=0;i<n;i++)
{
arr[i] = sc.nextInt();
}
return arr;
}
public static int removeDuplicate(int arr[])
{
int n = arr.length;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
/*for(int k=j;k<n;k++)//Shifting the values to left.
{
arr[k]=arr[k+1];
}*/
arr[j] = arr[n-1]; //This will exchange the values with the least one.
j--;
n--;
}
}
}
return n;
}
public static void displayFinalArray(int arr[], int n)
{
System.out.println("Final Array After Removing Duplicate Elements : ");
for(int i=0;i<n;i++)
{
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
int arr[] = readArr();
int n = removeDuplicate(arr);
System.out.println("The Size of Final Array is : "+n);
displayFinalArray(arr,n);
}
}