advent-of-code/hacker-rank/lonely-integer/main.go
xuu 04bbac8559
Some checks failed
Go Bump / bump (push) Failing after 6s
Go Test / build (push) Successful in 47s
chore: add hackerrank solutions
2024-10-26 12:03:06 -06:00

78 lines
1.4 KiB
Go

package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
/*
* Complete the 'lonelyinteger' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY a as parameter.
*/
func lonelyinteger(a []int32) int32 {
m := make(map[int32]int8, len(a)/2+1)
for _, i := range a {
m[i] = m[i]+1
}
for k,v := range m {
if v == 1 {
return k
}
}
return 0
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
checkError(err)
defer stdout.Close()
writer := bufio.NewWriterSize(stdout, 16 * 1024 * 1024)
nTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
n := int32(nTemp)
aTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
var a []int32
for i := 0; i < int(n); i++ {
aItemTemp, err := strconv.ParseInt(aTemp[i], 10, 64)
checkError(err)
aItem := int32(aItemTemp)
a = append(a, aItem)
}
result := lonelyinteger(a)
fmt.Fprintf(writer, "%d\n", result)
writer.Flush()
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}