Filter an Array HackerRank Solution

Hello Programmers, In this post, you will know how to solve the Filter an Array HackerRank Solution. This problem is a part of the HackerRank Linux Shell Series.

Filter an Array HackerRank Solution
Filter an Array HackerRank Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Filter an Array HackerRank Solution

Objective

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.

Resources
Here’s a great tutorial with useful examples related to arrays in Bash.

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.

Sample Input

Namibia
Nauru
Nepal
Netherlands
NewZealand
Nicaragua
Niger
Nigeria
NorthKorea
Norway

Sample Output

Niger

Explanation

Niger is the only name that does not contain an ‘a‘ or ‘A‘.

Filter an Array HackerRank Solution

# 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'.
arr=($(cat))
echo ${arr[@]/*[aA]*/}

Note: This problem (Filter an Array) is generated by HackerRank but the Solution is Provided by  BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: ‘Grep’ #1 HackerRank Solution

Leave a Reply

Your email address will not be published. Required fields are marked *