Code Logo

Count Finished Tasks

Published at19 Apr 2026
Loops & Iteration Easy 5 views
Like0

Given an array of task statuses where each element is 1 (finished) or 0 (not finished), count how many tasks are finished. Return the total count of completed tasks.

For example, statuses [1, 0, 1, 1, 0] has 3 finished tasks. All zeros [0, 0, 0] has 0 finished tasks. All ones [1, 1, 1] has 3 finished tasks. An empty array returns 0.

Counting items that match a condition is a core data aggregation skill. It appears in project management (tracking completed milestones), inventory systems (counting available items), and quality assurance (counting passed tests).

The solution initializes a counter to 0, iterates through each status, and increments when the value equals 1. This accumulates the total count in a single pass with O(1) extra space.

Edge cases include an empty array (return 0), all tasks unfinished (return 0), and all tasks finished (count equals array length).

Learn about our pseudocode specification
Guide

Example Input & Output

Example 1
Input
tasks = [1,0,1,1]
Output
3
Explanation

Three tasks are marked as finished.

Example 2
Input
tasks = []
Output
0
Explanation

An empty task list means there is nothing finished to count.

Example 3
Input
tasks = [0,0,0]
Output
0
Explanation

If nothing is finished, the count stays 0.

Algorithm Flow

Recommendation Algorithm Flow for Count Finished Tasks

Solution Approach

Iterate through the array and count elements equal to 1.

function countFinished(tasks)
  count = 0
  for each t in tasks
    if t == 1 then count = count + 1
  return count

Initialize count to 0. Loop through each task status. If the status equals 1 (indicating a finished task), increment the counter. After processing all tasks, return the total count. The algorithm correctly returns 0 for empty arrays since the loop never executes.

Time complexity is O(n), space complexity is O(1). Each element is examined exactly once.

Best Answers

Pseudocode - Approach 1
program count_finished_tasks
dictionary
   tasks: array[1..100] of integer
   finished_count, i, n: integer
algorithm
   input(tasks)
   finished_count <- 0
   n <- tasks.length
   for i <- 0 to n - 1 do
      if tasks[i] = 1 then
         finished_count <- finished_count + 1
      endif
   endfor
   output(finished_count)
endprogram