forked from ociule/codeeval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst_non_repeated.go
48 lines (43 loc) · 937 Bytes
/
first_non_repeated.go
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
package main
import "fmt"
import "log"
import "bufio"
import "os"
import "strings"
type RunePosCount struct {
Pos, Count int
}
func getFirst(in string) string {
nonRepeated := make(map[rune]RunePosCount, len(in))
for ix, char := range in {
val, ok := nonRepeated[char]
if !ok {
nonRepeated[char] = RunePosCount{ix, 1}
} else {
nonRepeated[char] = RunePosCount{val.Pos, val.Count + 1}
//fmt.Println(string(char), ix, val.Count + 1)
}
}
//fmt.Println(nonRepeated)
lowestPos := len(in)
out := rune(0)
for char, posCount := range nonRepeated {
if posCount.Count == 1 && posCount.Pos < lowestPos {
out = char
lowestPos = posCount.Pos
}
}
return string(out)
}
func main() {
file, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
fmt.Println(getFirst(line))
}
}