From 40da3403dabd1ab5325fae479dfadf20bcc15a18 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 18 Sep 2024 19:20:24 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB179.=20Largest=20Number=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/largest-number.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 go/largest-number.go diff --git a/go/largest-number.go b/go/largest-number.go new file mode 100644 index 0000000..fbd3f04 --- /dev/null +++ b/go/largest-number.go @@ -0,0 +1,29 @@ +package main + +import ( + "cmp" + "slices" + "strconv" +) + +func largestNumber(nums []int) string { + numbers := make([]string, len(nums)) + for i, num := range nums { + numbers[i] = strconv.Itoa(num) + } + + slices.SortFunc(numbers, func(a, b string) int { + return cmp.Compare(b+a, a+b) + }) + + if numbers[0] == "0" { + return "0" + } + + largest := "" + for _, number := range numbers { + largest += number + } + + return largest +}