Frequency of Numbers
Develop a Python application that will randomly select n integers from 1 to 9 and will count the number of occurrence of the numbers without using the Counter from collections library.
Sample Output:
How many numbers?: 7
[2, 6, 8, 2, 1, 1,6]
2-2
2-6
1-8
2-1
import random
n = int(input("How many numbers?: "))
arr=[]
rep=[]
for i in range(n):
arr.append(random.randint(1, 9))
print(arr)
for i in range(n):
cnt = 0;
for j in range(i,n):
if arr[i] == arr[j]:
cnt += 1
if arr[i] not in rep:
rep.append(arr[i])
print(f"{cnt} - {arr[i]}")
Comments
Leave a comment