-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
80 lines (72 loc) · 2.21 KB
/
Solution.java
File metadata and controls
80 lines (72 loc) · 2.21 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
73
74
75
76
77
78
79
80
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
import java.util.Scanner;
public class Solution {
public static int removeDuplicatesSoln(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
public static int removeDuplicates(int[] nums) {
int i = 0;
while (i < (nums.length - 1)) {
int p = nums[i];
while ((i < (nums.length - 1)) && (p == nums[i + 1])) {
nums[++i] = 101;
}
i++;
}
boolean flag = false;
i = 0;
int p = 0;
int k = 1;
while (i < nums.length) {
if (nums[i] == 101 && !flag) {
p = i;
flag = true;
}
else if ((nums[i] != 101) && flag) {
nums[p] = nums[i];
nums[i] = 101;
flag = false;
i = p;
k++;
}
else if (nums[i] == 101) {
k++;
}
i++;
}
return k;
}
public static void main(String[] args) {
// taking input
if (args != null) {
int temp = Integer.parseInt(args[0]);
if ((temp > (3 * Math.pow(10, 4))) || (temp < 0))
throw new IllegalArgumentException();
Scanner sc = new Scanner(System.in);
int[] a = new int[temp];
for (int i = 0; i < temp; i++) {
int n = sc.nextInt();
if ((n > 100) || (n < (-100)))
throw new IllegalArgumentException();
else
a[i] = n;
}
int k = removeDuplicatesSoln(a);
System.out.println(k);
for (int i = 0; i < a.length; i++)
System.out.println(a[i]);
}
}
}