LeetCode - Arrays - Element Appearing More Than 25% In Sorted Array

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time.

Return that integer.

Example 1:

Input: arr = [1,2,2,6,6,6,6,7,10]
Output: 6
class Solution {
    public int findSpecialInteger(int[] arr) {
        Map<Integer,Integer> freqMap = new HashMap<Integer,Integer>();
        for(int i : arr){
            freqMap.put(i,freqMap.getOrDefault(i,0)+1);
        }
        for(int i:freqMap.keySet()){
            int val = freqMap.get(i);
            double percent = ((double)val*100)/arr.length;
            if(percent > 25) return i;
        }
        return 0;
    }
}

Recent Updates

Recent updates on DevOpsPal

Social

Follow us of social for latest updates.

DevOps Hub

Links to get started.