-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicates
More file actions
49 lines (40 loc) · 976 Bytes
/
Copy pathRemoveDuplicates
File metadata and controls
49 lines (40 loc) · 976 Bytes
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
package arrayImpl;
import java.util.Arrays;
public class RemoveDuplicates {
public static int[] removeDuplicates(int arr[])
{
//assuming all elements are unique
int n=arr.length;
//Comparing each element with all other elements
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
//Replace duplicate element with last element
arr[j]=arr[n-1];
n--;
j--;
}
}
}
//Copying only unique elements of arrayWithDuplicates into arrayWithoutDuplicates
int arr1[]=Arrays.copyOf(arr,n);
return arr1;
}
public static void main(String[] args) {
int arr[]={2,4,6,3,5,9,4};
System.out.println("Array with duplicates");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]+" ");
}
int arr1[]= removeDuplicates(arr);
System.out.println("After removing the duplicates");
for(int i=0;i<arr1.length;i++)
{
System.out.println(arr1[i]+" ");
}
}
}