The most occurring number in a string using Regex – Python
Last Updated :
10 Apr, 2025
Improve
We are given a string that contains a mix of text and numbers, and our task is to find the number that appears most frequently. For example, in the string “I have 3 apples, 12 oranges, 3 bananas, and 15 3”, the number “3” appears the most. To solve this efficiently, we can use regular expressions (Regex) in Python to extract all numbers from the string and then determine which one occurs most often. Let’s explore an efficient approach to solve this.
Step-by-Step Algorithm
- Step 1: Find and collect all the digit sequences from the string. Store them as a list of strings, like [‘3′, ’12’, ‘3’, ’15’, ‘3’].
- Step 2: Count how many times each number appears in the list.
- Step 3: Look through the counts to find the number that appears the most. If two or more numbers have the same highest count, pick the larger one.
- Step 4: Convert the most frequent number to an integer and return or print it.
Code Implementation
import re
from collections import Counter
s = 'geek55of55gee4ksabc3dr2x'
# Extract digit sequences
a = re.findall(r'\d+', s)
# Count frequencies
freq = Counter(a)
# Track max frequency and number
mx, res = 0, 0
for x in freq:
if freq[x] >= mx:
mx = freq[x] # update max frequency
res = int(x) # update result with larger num in tie
print(res)
Output
55
Explanation:
- re.findall(r’\d+’, s) extracts all continuous digit sequences from the string s, returning: [’55’, ’55’, ‘4’, ‘3’, ‘2’].
- Counter(a) creates a frequency dictionary from the list a, for example: {’55’: 2, ‘4’: 1, ‘3’: 1, ‘2’: 1}.
- mx keeps track of the highest frequency found so far and res stores the corresponding number (converted to int).
- The loop goes through each number x in the frequency dictionary. If freq[x] ≥ mx, it updates mx and sets res = int(x) so that the largest number is selected in case of a tie.