From 39519f179b8a923140e696e9bcf2dc3b34d7d6f8 Mon Sep 17 00:00:00 2001 From: Muhammad Khuzaima Umair Date: Mon, 11 Dec 2023 19:52:35 +0500 Subject: [PATCH] Create 1287_Element_Appearing_More_Than_25%_In_Sorted_Array.java --- ...pearing_More_Than_25%_In_Sorted_Array.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1287_Element_Appearing_More_Than_25%_In_Sorted_Array.java diff --git a/1287_Element_Appearing_More_Than_25%_In_Sorted_Array.java b/1287_Element_Appearing_More_Than_25%_In_Sorted_Array.java new file mode 100644 index 0000000..41c6d52 --- /dev/null +++ b/1287_Element_Appearing_More_Than_25%_In_Sorted_Array.java @@ -0,0 +1,23 @@ +// id: 1287 +// Name: Element Appearing More Than 25% In Sorted Array +// link: https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/ +// Difficulty: Easy + +class Solution { + public int findSpecialInteger(int[] arr) { + int c = arr.length / 4; + + int current = arr[0]; + int count = 0; + for (int i: arr) { + if (i == current) { + count++; + if (count > c) return i; + } else { + current = i; + count = 1; + } + } + return -1; + } +}