Basic bash concepts, Part-10 (Arrays) with solved Hackerrank problems

Before this post, you have learned

  1. Basic bash hackerrank
  2. Shell beginner
  3. Bash II chapter
  4. Bash III chapter
  5. Bash IV chapter
  6. Bash V chapter
  7. Bash VI chapter
  8. Bash VII chapter
  9. Bash Part 8 Array
  10. Bash Part 9 Array

Basic bash concepts, Part-10 (Arrays)

Here we will provide  Hackerrank question and solution, You should check previous post            “Bash Part 9 Array“, Because we have already  discussed basic introduction about array with syntax.

Problem Solver:

Q1.

Given a list of countries, each on a new line, your task is to read them into an array and then display the element indexed at 3. Note that indexing starts from 0.

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

The element at index of the array (one string).

Sample Input

Namibia
Nauru
Nepal
Netherlands
NewZealand
Nicaragua
Niger
Nigeria
NorthKorea
Norway

Sample Output

Netherlands

Explanation

The element at index in the list is Netherlands.

while read line

do array=(“${array[@]}” $line) done echo ${array[3]}

here You can see that a[i] mean value at index i of array a, we have to search 3rd element ,index of array.

Q2.

 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.

Sample Input

Namibia
Nauru
Nepal
Netherlands
NewZealand
Nicaragua
Niger
Nigeria
NorthKorea
Norway

Sample Output

10

Explanation

There are 10 elements in the array – i.e. the given list has the names of 10 countries.

while read line

do array=(“${array[@]}” $line) done echo ${#array[@]}

Q3.

 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.

while read line

do array=(“${array[@]}” $line) done echo ${#array[@]}Q4. You are given a list of countries, each on a new line. Your task is to read them into an array and then transform them in the following way:

The first capital letter (if present) in each element of the array should be replaced with a dot (‘.‘). Then, display the entire array with a space between each country’s names.

Objective
In this challenge, we practice reading and transforming arrays.

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

Transform the names as described and display the entire array of country names with a space between each of them.

Sample Input

Namibia
Nauru
Nepal
Netherlands
NewZealand
Nicaragua
Niger
Nigeria
NorthKorea
Norway

Sample Output

.amibia .auru .epal .etherlands .ewZealand .icaragua .iger .igeria .orthKorea .orway

Explanation

The first capital letter of each name has been replaced with a dot (‘.‘).

while read line
do array=(“${array[@]}” $line) done let lin=${#array[@]} x=0 until [ $x -eq $lin ] do arr=(“${arr[@]}” ${array[$x]/${array[$x]:0:1}/.}) let x+=1 done echo ${arr[@]} or while read line do array=(“${array[@]}” $line) done echo ${array[@]/[A-Z]/.}
Q5.

 There are N integers in an array A. All but one integer occur in pairs. Your task is to find the number that occurs only once.

Input Format

The first line of the input contains an integer N, indicating the number of integers. The next line contains N  space-separated integers that form the array A.

Constraints

1<=N<100

N%2=1(N is an odd number)

0<= A[i] <= 100 , i is  1 to N.

Output Format

Output , the number that occurs only once.

Basic bash concepts, Part-10 (Arrays)

Explanation

In the first input, we see only one element (1) and that element is the answer.
In the second input, we see three elements; 1 occurs at two places and 2 only once. Thus, the answer is 2.
In the third input, we see five elements. 1 and 0 occur twice. The element that occurs only once is 2.

Solution
read N
read -a arr
let x=${#arr[@]}
for (( i=0; i<$x; i++ ))
do 
count=0
for (( j=0; j<$x; j++))
do
    if [ ${arr[$i]} -eq ${arr[$j]} ]  
    then
    let count+=1
    fi
done
    if [ $count -eq 1 ] 
    then
     echo ${arr[$i]}
    fi
done

because this is an array problem although we can solve this without array.

read
echo $(( `sed 's/ /^/g'` ))

or

read
echo $(( `tr ' ' '^'` ))



or


read
echo $(( `sed 's/ /^/g'` ))

We can use XOR operator also Code Credit @ @pranitbose_15

read
arr=($(cat))
arr=${arr[*]}
echo $((${arr// /^}))
read q
arr=($(cat))
k=${#arr[@]}

flag=0
for(( i=0; i<k; i++ ))
do
    for(( j=0; j<k; j++ ))
    do
    if [ ${arr[i]} -eq ${arr[j]} ]
    then
    flag=$((flag+1))
    fi
    done
if [ $flag -eq 1 ]
then
echo -n ${arr[i]}" "
fi
flag=0
done

and Code Credit @vicch7

read n                 # Actually no use of this
array=($(tr ' ' '\n')) # Read numbers into array
result=0               # Result of XOR operations
for value in ${array[@]}; do
    result=$[$result ^ $value] # XOR each number with current result
done
echo $result

That’s it for this post. enjoy and code.

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.