-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveduplicates.java
More file actions
72 lines (55 loc) · 1.28 KB
/
Removeduplicates.java
File metadata and controls
72 lines (55 loc) · 1.28 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Java Program to Remove Duplicate Elements
// From the Array using HashMap
import java.util.HashMap;
class Removedupliactes {
static void removeDups(int[] a, int n)
{
// Hash map which will store the
// elements which has appeared previously.
HashMap<Integer, Boolean> mp = new HashMap<>();
for (int i = 0; i < n; ++i) {
// Print the element if it is not
// present there in the hash map
// and Insert the element in the hash map
if (mp.get(a[i]) == null)
{
System.out.print(a[i] + " ");
mp.put(a[i], true);
}
}
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 1, 2, 5, 1, 7, 2, 4, 2 };
int n = arr.length;
removeDups(arr, n);
}
}
/*public class Removeduplicates {
public static int removeDuplicates(int a[], int n)
{
// if(array size if 0 or 1 array is already sorted)
if (n == 0 || n == 1) {
return n;
}
int j = 0;
for (int i = 0; i < n - 1; i++) {
if (a[i] != a[i + 1]) {
a[j++] = a[i];
}
}
a[j++] = a[n - 1];
return j;
}
public static void main(String[] args)
{
int a[] = { 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6 ,6};
int n = a.length;
int j=0;
j = removeDuplicates(a, n);
// printing array elements
for (int i = 0; i < j; i++)
System.out.print(a[i] + " ");
}
}*/