Write an algorithm to search for and count the number of times a value is found in a list L of n numbers. Each item in the list can be accessed by L[i] where i is the position of the item in the list. In other words, L[O] is the first item, L[1] is the second item, and so on. The last item can be accessed by! L[n-1]. The user enters the value to search for and count, then your algorithm should print the number of occurrences of the value in the list. If it is not there, it will print "The value was not found".
Aflgorithm:
-->l=list of n numbers;
-->x=input(); /*number to search*/
-->count=0;
-->for i=0 to n:
if l[i]==x:
count=count+1
-->if count==0:
display "the value was not found"
else:
disply count
--------------------------------------------------------------------
python code:
l=[1,2,3,2,4,3,5,2,3,4]
x=int(input("enter the number to search the no.of occurence:
"))
c=0
for i in range(len(l)):
if(l[i]==x):
c=c+1
if(c==0):
print("the value is not found")
else:
print("No.of occurence is ",c)
------------------------------------------------
input:3
Output:3
Hope you understand.If you have any doubts comment me.Upvote me.
Get Answers For Free
Most questions answered within 1 hours.