Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Count the number of elements in an array problem solution

YASH PAL, 31 July 2024

In this HackerRank Count the number of elements in an array problem solution we have given a list of countries, each on a new line, your task is to read them into an array and then display the count of elements in that array.

Input Format

A list of country names. The only characters present in the country names will be upper or lower-case characters and hyphens.

Output Format

A single integer – the number of elements in the array.

HackerRank Count the number of elements in an array problem solution

Problem solution.

arr=($(cat))
echo ${#arr[@]}

Second solution.

num=0
while read cnt; do num=$((num + 1)); done
echo $num

Third solution.

#!/bin/bash

n=0
while read line
do
tab[n]=$line
n=$((n+1))
done

echo ${#tab[@]}

Fourth solution.

while read LINE
do
 my_array=("${my_array[@]}" $LINE)
done

echo "${#my_array[@]}"

coding problems solutions linux shell

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes