Header Ad

HackerRank Count the number of elements in an array problem solution

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[@]}"


Post a Comment

0 Comments