Find the most occurring element and its frequency in an array.
1Times asked
Jul 2026Last seen
Jul 2026First seen
💡 Model Answer
You can solve this by counting occurrences with a dictionary (hash map). Iterate through the array, increment counts, then find the key with the maximum value. Complexity is O(n) time and O(k) space, where k is the number of distinct elements.
python
from collections import Counter
def mode_and_count(arr):
if not arr:
return None, 0
counts = Counter(arr)
mode, freq = counts.most_common(1)[0]
return mode, freq
# Example
print(mode_and_count([1, 2, 2, 3, 3, 3])) # Output: (3, 3)This code handles empty arrays and returns the element with the highest frequency along with its count.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500