-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniqelem.java
More file actions
54 lines (43 loc) · 1.49 KB
/
uniqelem.java
File metadata and controls
54 lines (43 loc) · 1.49 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
/**
(c) Sergio Morales 2013
CodeEval Challenge: Unique Elements
Dated Solved: 12/22/13
**/
import java.io.*;
public class Main {
public static boolean found(String item, String[] list) {
for(int i = 0; i < list.length; ++i) {
if(list[i] != null && list[i].equals(item)) {
return true;
}
}
return false;
}
public static void main (String[] args) throws IOException {
File file = new File(args[0]);
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while( (line = in.readLine()) != null) {
String[] lineArray = line.split(",");
String[] uniqueArray = new String[lineArray.length];
Integer count = 0;
for(int i = 0; i < lineArray.length; ++i) {
if(!found(lineArray[i], uniqueArray)) {
uniqueArray[count] = lineArray[i];
++count;
}
}
for(int i = 0; i < uniqueArray.length; ++i) {
if(uniqueArray[i] != null) {
System.out.print(uniqueArray[i]);
if( (i+1 < uniqueArray.length-1) ) {
if(uniqueArray[i+1] != null) {
System.out.print(",");
}
}
}
}
System.out.println();
}
}
}