Skip to main content

1287. Element Appearing More Than 25% In Sorted Array

Hi, Today i bring you new Leetcode problem. Its an easy one :)

The problem is - Element Appearing More Than 25% In Sorted Array

As qs. explains itself we need to find a number that appears 25% times or more than the length of he array. So for example if array has length of 8 then the resultant number should appear 2 or more times.

The constraints given are- 1 <= arr.length <= 100, 0 <= arr[i] <= 105

So, lets look at the cases which we may encounter.

1. if length is 1. then the number itself is the result.

2. if length is divisibe by 4

3. if length is not divisible by 4.

Let's look at the code now...

 class Solution:
def findSpecialInteger(self, arr: List[int]) -> int:
n = len(arr)
start = 0
end = n

hm = defaultdict()

for i in arr:
hm[i] = hm.get(i, 0)+1
for i in hm:
if hm[i]>n//4:
return i

Now we will go through the code.

Step 1: Initialization

n = len(arr) start = 0 end = n hm = defaultdict()
  • n is the length of the input list arr.
  • start and end are initialized but not used in the provided code snippet.
  • hm is a dictionary that will be used to count the occurrences of elements in the list. It's created using defaultdict(), meaning that if a key is not found, it defaults to a count of 0.

Step 2: Counting Occurrences

for i in arr: hm[i] = hm.get(i, 0) + 1
  • This loop goes through each element i in the input list arr.
  • For each element i, it increments the count of i in the dictionary hm. If i is not already a key in hm, it initializes the count to 1. If i is already a key, the current count is incremented.

Step 3: FindingValue which occurs more than 25%.

for i in hm: if hm[i] > n // 4: return i
  • This loop goes through each key i in the dictionary hm.
  • For each key i, it checks if the count of i is greater than a quarter (25%) of the length of the input list (n // 4).
  • If the count of any element is greater than a quarter of the length of the input list, the method returns that elemen.

Here how this code performed.

 

Hope you like it. Happy coding

Comments

Popular posts from this blog

What is a Processor?

Smartphones have been the most important part of our lives. We have seen the evolution of our  handsets. First we saw landlines which were mess of tangled wires then we saw mobile phones which is successor of landlines. They were portable devices from which you can make calls then we witnessed emergence of Smartphones which were  considered as portable computers which also allows us to make calls. What is a Processor? Underneath that touchscreen display there is a full- fledged computer that commands your apps so that they function properly. A processor executes what you want your smartphone to do or in other word you can say processor is brain of computer. there is Exynos, Snapdragon, quad-core octa-core etc. What is a core? It is an element of surprises. They are present inside processor and are responsible for reading the command and executing them. First devices came with single core processor but later on scientists invented more advanced processors such as dual core , So...

Why 'C' is the default hard disk drive name

Computers have been really important part of our lives. Making our work easy and complete it in no time is the main advantage of computers. In the beginning computers were really expensive and were used by few people. Then with the increase and ease in technology computers became affordable and now-a-days everyone is using computers. Computers are proved as an evolution in our lives. Though you may be using computers but some of you may not be aware of some hidden facts. If you are using computers daily so here is one simple question for you. " Do you know why 'C' is the default hard dis drive and not 'A' or 'B'? Here is the answer : Hard disk drives became standard since 1980, Before Hard disk drives Floppy disk drivers were used as storage devices. Floppy Disk drives were initial storage devices. They came to existence in 1960's. They came in mainly 2 sizes, 5 1/4" and 3 1/2". These two floppy disk drives were labelled as Local Disk (A) and ...

WhatsApp hidden Fonts

WhatsApp is a messaging app used by over 1 million world wide. It have been the default messaging app for the people since it was first made, Social messaging app supports Android, IOS and windows. It surely have become crucial part of our lives but some of us may not know about some of its hidden features. Whatsapp gives its user full liberty to block useless contacts, choose wisely to let other people know whether you read their messages or not (read receipts), setting privacy, secret fonts, and recent updates gave us the option to post stories too, no doubt why it is number 1 messaging app. Some of us may know the feature of hidden text and some of us may not, they are very simple. If you ever wanted to highlight the particular text or say make it bold so you can do that on whatsapp Here are some cool tricks that you can do while typing messages to your friends. Italicize the text by placing asterisks before and after the text. _techgen_ make the text appear bold by placing tidle sy...