-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowestUniqueNumber.java
More file actions
55 lines (46 loc) · 1.21 KB
/
LowestUniqueNumber.java
File metadata and controls
55 lines (46 loc) · 1.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class LowestUniqueNumber {
String filename;
public LowestUniqueNumber(String filename) {
this.filename = filename;
}
public void findUnique() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
int i;
while ((line = br.readLine()) != null) {
int[] counts = new int[10];
int lowestUnique = 0;
String[] nums = line.split("\\s");
for (i = 0; i < nums.length; i++) {
counts[Integer.parseInt(nums[i]) - 1]++;
}
for (i = 0; i < counts.length; i++) {
if (counts[i] == 1) {
lowestUnique = i + 1;
break;
}
}
if (lowestUnique != 0) {
for (i = 0; i < nums.length; i++) {
if (lowestUnique == Integer.parseInt(nums[i])) {
break;
}
}
System.out.println(i+1);
} else {
System.out.println(lowestUnique);
}
}
}
public static void main (String[] args) throws IOException {
if (args.length != 1) {
System.out.println("Unsupported number of parameters passed. Exiting.");
System.exit(1);
}
LowestUniqueNumber lun = new LowestUniqueNumber(args[0]);
lun.findUnique();
}
}