forked from smanaqvi83/Java-Interview-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemovingDuplicates.java
More file actions
39 lines (30 loc) · 958 Bytes
/
Copy pathRemovingDuplicates.java
File metadata and controls
39 lines (30 loc) · 958 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
package com.sky.pgm;
import java.util.Arrays;
public class RemovingDuplicates {
public static void main(String[] args) {
int[][] test = new int[][]{
{1, 1, 2, 2, 3, 4, 5},
{1, 1, 1, 1, 1, 1, 1},
{1, 2, 3, 4, 5, 6, 7},
{1, 2, 1, 1, 1, 1, 1},};
for (int[] input : test) {
System.out.println("Array with Duplicates : " + Arrays.toString(input));
System.out.println("After removing duplicates : " + Arrays.toString(removeDuplicates(input)));
System.out.println();
}
}
private static int[] removeDuplicates(int[] input) {
Arrays.sort(input);
int[] result = new int[input.length];
int prev = input[0];
result[0] = prev;
for (int i = 0; i < input.length; i++) {
int curr = input[i];
if(prev != curr){
result[i] = curr;
}
prev = curr;
}
return result;
}
}