-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayAbsurdity.java
More file actions
51 lines (40 loc) · 1.14 KB
/
Copy pathArrayAbsurdity.java
File metadata and controls
51 lines (40 loc) · 1.14 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
/**
*Imagine we have an immutable array of size N which we know to be filled with integers
*ranging from 0 to N-2, inclusive. Suppose we know that the array contains exactly one
*duplicated entry and that duplicate appears exactly twice. Find the duplicated entry.
*(For bonus points, ensure your solution has constant space and time proportional to N)
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
public class array_absurdity {
public static Stack<String> has = new Stack<String>();
public static void main(String[] args) {
read(args[0]);
}
private static void proc(String s) {
if (s.length() > 0) {
String[] first = s.split(";");
Scanner in = new Scanner(first[1]).useDelimiter(",");
while (in.hasNext()) {
String sr = in.next();
if (has.search(sr) > 0) {
System.out.println(sr);
} else
has.add(sr);
}
}
has.clear();
}
public static void read(String f) {
File file = new File(f);
try {
Scanner in = new Scanner(file);
while (in.hasNextLine()) {
proc(in.nextLine());
}
} catch (FileNotFoundException e) {
}
}
}