How to count even and odd frequencies in a number stream
Counting the frequency of numbers in a dataset or a number stream is a fundamental operation in data analysis and statistics. In real life, this frequency counting algorithm is applied to solve complex business problems.
For example, consider an inventory store where customers purchase their daily use products. We can utilize frequency counting to identify the best selling products, track customer visit frequencies, monitor product stockout frequencies, and analyze purchase patterns over time.
In this Answer, we’ll take a stream of numbers and count the frequencies of both even and odd numbers in it.
Even and odd frequencies
We’ll use two variables to count even and odd numbers within the stream of numbers. We’ll iterate through the entire stream, checking each number to determine whether it’s even or odd. If it’s even, we’ll increment the even count variable by 1, and if it’s odd, we’ll increment the odd count variable by 1.
Code example 1
Here’s a code example:
# Initialize count variablesevenCount = 0oddCount = 0# Define a stream of numbersnumberStream = [2, 5, 8, 7, 10, 13, 6, 3]# Iterate the number streamfor number in numberStream:# If the number is even, increment evenCount by 1if number % 2 == 0:evenCount += 1else:# If the number is odd, increment oddCount by 1oddCount += 1# Display the frequncyprint("Even frequency:", evenCount)print("Odd frequency:", oddCount)
Code explanation
Lines 2–3: We initialized the
evenCountandoddCountvariables with0.Line 5: We list the
numberStreamarray initialized with the numbers.Lines 9–15: We use the
forloop to iterate through the list of numbers, checking each number. If the number is even, it increments theevenCountvariable by1and if the number is odd, it increments theoddCountvariable by1.Lines 18–19: We print the frequency of both even and odd numbers.
Code example 2
In this code example, we solve a problem using a function and AND method. We modify the code to check if each number in a list is even or odd.
def CountingEvenOdd(numberStream, size):evenCount = 0oddCount = 0for i in range(size): # Iterate the number streamif (numberStream[i] & 1 == 1): # If the number is odd, incerement oddCount by 1oddCount += 1else:evenCount += 1. # If the number is even, incerement evenCount by 1print("Number of even elements = ",evenCount)print("Number of odd elements = ",oddCount)numberStream = [2, 3, 4, 5, 6] # Define a stream of numbersn = len(numberStream)CountingEvenOdd(numberStream, n) # Call the function
Code explanation
Line 1: We define a function named
CountingEvenOdd()with two parameters:numberStream(a list of numbers) andsize(the size of the list).Line 5: We start a loop to iterate through each value in the
numberStreamlist.Line 6: We implement the condition: if the least significant bit of the number at index
iis 1, and the bitwise AND operation with1results in1, then the number at the indexiis odd; otherwise even.Line 18: We compute the length of the
numberStreamlist and assign it to the variablen.Line 21: We call the
CountingEvenOdd()function with thenumberStreamlist and its lengthnas arguments.
Free Resources