Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Filter an Array with Patterns problem solution

YASH PAL, 31 July 20246 February 2026

In this HackerRank Filter an Array with Patterns problem solution We now transition to some basic examples of bash scripting for the purpose of text processing and data munging. In this challenge, we practice reading and filtering an array.

Task

You are given a list of countries, each on a new line. Your task is to read them into an array and then filter out (remove) all the names containing the letter ‘a’ or ‘A’.

Input Format

The input format consists of a list of country names, each on a separate line. The only characters present in the country names will be upper or lower-case characters and hyphens.

Output Format

From the given list, remove the names that contain ‘a’ or ‘A’ in them. If there are no names left after removing these characters, you should display a blank line.

hackerrank filter an array with patterns problem solution

Problem solution.

readarray -t country
echo "${country[@]/*[aA]*/}"|sed 's/^s*//g'

Second solution.

while read n
do
a=("${a[@]}" "$n")
done
temp=( ${a[@]/*a*/} )
echo ${temp[@]/*A*/}

Third solution.

array=()
while read line; do
    array=(${array[@]} $line)
done
filtered=(${array[@]/*a*/})
echo ${filtered[@]}

Fourth solution.

iter=0
while [ 1 ]
do 
read line
if [ -n "$line" ]; then array[$((iter++))]="$line"
else break
fi 
done

for item in ${array[@]}
do
    if [ "$item" = "$(echo $item | grep -wE "[^aA]*")" ]; 
    then echo -n "$item " 
    fi
done

coding problems solutions Hackerrank Problems Solutions linux shell HackerRankLinux shell

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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