From 85181344a69bddf5e51b2ba7bc67f86a81d2b6fa Mon Sep 17 00:00:00 2001 From: ductnn Date: Mon, 11 Dec 2023 10:29:47 +0700 Subject: [PATCH] add sol --- ...AppearingMoreThan25percentInSortedArray.go | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 leetcode/1287.ElementAppearingMoreThan25percentInSortedArray/elementAppearingMoreThan25percentInSortedArray.go diff --git a/leetcode/1287.ElementAppearingMoreThan25percentInSortedArray/elementAppearingMoreThan25percentInSortedArray.go b/leetcode/1287.ElementAppearingMoreThan25percentInSortedArray/elementAppearingMoreThan25percentInSortedArray.go new file mode 100644 index 0000000..06d8384 --- /dev/null +++ b/leetcode/1287.ElementAppearingMoreThan25percentInSortedArray/elementAppearingMoreThan25percentInSortedArray.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" +) + +func findSpecialInteger(arr []int) int { + for k, v := range arr { + if v == arr[k+(len(arr)>>2)] { + return v + } + } + return 0 +} + +func findSpecialInteger1(arr []int) int { + if len(arr) < 4 { + return arr[0] + } + count := 1 + for i := 1; i < len(arr); i++ { + if arr[i] == arr[i-1] { + count++ + if count > len(arr)/4 { + return arr[i] + } + } else { + count = 1 + } + + } + return -1 +} + +func main() { + arr := []int{1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12} + fmt.Println(findSpecialInteger1(arr)) +}