Friday, 11 March 2022

Repetition Problem !!!

PROBLEM 44:  Repetition


A Program to find out the most repeated number in the array. (Without using built-in functions)

(Interview Question)


 Author - Ajay Zad 

 Date     - 11/03/2022 


n = int(input("Input the size of the array :"))

l = []

s = set()

for i in range(0,n):

    ele = int(input("Enter the elements in the array :"))

    l.append(ele)

    #Adding no. in set as it doesn't store duplicates

    s.add(ele)

    

cnt = 0

cnt1 = 0

l2 = list(s)

for i in range(0,len(l2)):

    cnt = 0

    for j in range(0,n):

        #Comparing for equality 

        if l2[i] == l[j]:

            cnt = cnt + 1

            #Condition for counter 

            if cnt > cnt1:

                cnt1 = cnt

                m_no = l2[i]

print("The number with highest repetition : ",m_no)


Input :

Input the size of the array : 5

Enter the elements in the array : 1

Enter the elements in the array : 2

Enter the elements in the array : 2

Enter the elements in the array : 3

Enter the elements in the array : 2


Output :

The number with highest repetition : 2


                


                

    

No comments:

Post a Comment

Rearrange an array with O(1) extra space

  PROBLEM 61:  Rearrange an array with O(1) extra space (For best view experience, view in windows version) Problem Reference : GeeksForGeek...