Print all repeating digits present in a given number in sorted order


Python has internal functions lie count, counter and operator functions that can be used for printing all the repeated digits present in a number that too in sorted order. The following example will help you understand the concept more clearly.

Example

Assume we have taken an input string. We will now print all repeating/duplicate digits present in a given input number in sorted order using the above methods.

Input

inputNum = 5322789124199

Output

1 2 9

In the above input number, 2, 1, and 9 are repeated digits. So these digits are sorted in ascending order.

Hence the output duplicate digits in sorted order are 1,2, 9.

Using the count() function

String count() function

Returns the no. of times the given value appears in a string.

Syntax

string.count(value, start, end)

sort() function

The sort() method sorts the original list in place. It signifies that the sort() method changes the order of the list's elements.

list.sort()

join() function

join() is a string function in Python that is used to join elements of a sequence that are separated by a string separator. This function connects sequence elements to convert to a string.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Create a variable to store the input number.

  • Initialize an empty list for storing resultant repeating digits in an input number.

  • Use the str() function to convert the input number to a string.

  • Use the for loop to traverse through each character in a string.

  • Use the if conditional statement to check whether the frequency of the current character is greater than 1 with the count() function and it is not present in a resultant duplicates list

  • Use the append() function(adds the element to the list at the end) to append that character to the resultant list(duplicate list) if the condition is true.

  • Use the sort() function to sort the resultant duplicate list in ascending order.

  • Use the join() function to convert the resultant duplicate list to a string and print it.

Example

The following program returns all repeating digits present in an input number in sorted order using the count() function –

# input number
inputNum = 5322789124199
# storing resultant repeating digits in a number in a list
duplicatesList = []
# converting input number to a string
newString = str(inputNum)
# traversing through each character if a string
for c in newString:
  # checking whether the frequency of the current character is greater than 1
  # and it is not present in a resultant duplicates list
    if(newString.count(c) > 1 and c not in duplicatesList):
        # appending that character to the duplicates list if the condition is true
        duplicatesList.append(c)
# sorting the resultant duplicates list
duplicatesList.sort()
# converting resultant duplicates list to string
print(' '.join(duplicatesList))

Output

On execution, the above program will generate the following output –

1 2 9

Using Hashing

The following program returns all repeating digits present in an input number in sorted order using Hashing –

Example

def getDuplicateNum(inputNum):
    # setting the count of all the digits (from 0 to 9) to 0
    count = [0] * 10
    # converting the input number to a string
    newString = str(inputNum)
    # traversing through each character of a string
    for c in newString:
        # getting the integer value of the current character
        curr_digit = int(c)
        # incrementing the count of digits by 1
        count[curr_digit] += 1
    # Traversing in frequency(count)
    for k in range(10):
        # checking whether the frequency of the digit is greater than 1
        if (count[k] > 1):
            # printing that digit of the count is greater than 1
            print(k, end=" ")
# input number
inputNum = 5322789124199
# calling the above defined getDuplicateNum() by passing
# input number as an argument to it
getDuplicateNum(inputNum)

Output

1 2 9 

Using the Counter() function

The following program returns all repeating digits present in an input number in sorted order using the Counter() function –

Example

# importing Counter from the collections module
from collections import Counter
# input number
inputNum = 5322789124199
# storing resultant repeating digits in a number in a list
duplicatesList = []
# converting input number to a string
newString = str(inputNum)
# getting the frequency of each character of a string as a key-value pair
charFrequency = Counter(newString)
# traversing through the key, value pairs of characters frequency
for k, v in charFrequency.items():
  # checking whether current value is greater than 1(repeating)
    if v > 1:
        # appending that corresponding key to the duplicates list if the condition is true
        duplicatesList.append(k)
# sorting the resultant duplicates list
duplicatesList.sort()
# converting resultant duplicates list to string
print(' '.join(duplicatesList))

Output

1 2 9

Using the operator.countOf() function

operator.countOf function

The countOf() function of the operator module returns the number of elements in a that are equal to b.

Syntax

operator.countOf(a, b)

Parameters

  • a − list or string or any other data type.

  • b − value for which we must count the number of occurrences in “a”

The following program returns all repeating digits present in an input number in sorted order using the operator.countOf() function –

Example

import operator as op
# input number
inputNum = 5322789124199
# storing resultant repeating digits in a number in a list
duplicatesList = []
# converting input number to a string
newString = str(inputNum)
# traversing through each character if a string
for c in newString:
  # checking whether the frequency of the current character is greater than 1
  # and it is not present in a resultant duplicates list
    if(op.countOf(newString, c) > 1 and op.countOf(duplicatesList, c) == 0):
        # appending that character to duplicates list if the condition is true
        duplicatesList.append(c)
# sorting the resultant duplicates list
duplicatesList.sort()
# converting resultant duplicates list to string
print(' '.join(duplicatesList))

Output

1 2 9

Conclusion

We learned how to print all repeated digits in a given number in sorted order in this article. Using the new function was another thing we learned operator.countOf() function to determine how many elements are there in an iterable

Updated on: 18-Aug-2023

415 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements