Q:

Python program to find the occurrence of a particular number in an array

belongs to collection: Python Array Programs

0

ARRAY: An array can be defined as a container object that is used to hold a fixed number of values of a single type of data. The main purpose of an array is to store multiple items of the same type together.

arrays in python

Suppose you are given with an array that contains ints. Your task is to return the number of 3 in the array.

Example:

    Count3([1, 2, 3]) = 1
    Count3([1, 3, 3]) = 2
    Count9([1, 3, 9, 3, 3]) = 3

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Here we have to again consider a variable which is initially equal to zero to keep the count of a number of 3 and also our function is only defined for nums as our array would only contain integers as mentioned earlier.

Code:

def Count3(nums):
    count = 0
    for num in nums:
        if num == 3:
            count = count + 1

    return count

print (Count3([1,2,3,4,3,3,3,]))

Output

4

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python program to find the largest element in an a... >>
<< Python program to find a series in an array consis...