In this blog, we will understand how to find uncommon characters in two strings. For example, String1=”iamcoder” and String2=”iamloser” has below uncommon characters .c, d, l, s str1=”iamcoder” # String 1str2=”iamloser” # String 2CharInStr1NtPrInStr2=” #Blank String which will have Characters in String1 which are not present in String2CharInStr2NtPrInStr1=” #Blank String which will have Characters in String2 which are not present in String1m=${#str2} ##Length of a String2n=${#str1} ##Length of a String1i=0j=0while [[ $i -lt $n ]] #loop through all characters of String1doChar=${str1:$i:1}CharPresentInStr1=echo $str2 | grep -i $Char | wc -l #checking…

# Tag: if in shell

## Get all distinct Character in a string

In this blog, Sharing code to get only unique Characters present in a string. Please do not get confused with my Previous Blog . Its a different topic all together. Consider a String “allabouttechnologies“, so the distinct or unique Characters of this String are “alboutechngis“. str=”alllabouttechnologies” ##Sample Stringnewstr=” ##Blank new Stringn=${#str} ## Length of a Stringi=0while [[ $i -le $n ]] ##looping Character by CharacterdoChar=${str:$i:1}CharCntInNewStr=echo ${newstr}| grep ${Char} | wc -l ##Checking if Character already exists in new Stringif [[ $CharCntInNewStr -eq “0” ]]thennewstr=${newstr}${str:$i:1} ## appending Character if it is…

## kth smallest element in array

This is continuation of sorting of array elements. Please go through this blog . In that blog, sorting of array elements is explained along with code. In order to find out kth smallest or kth largest element in array having all distinct elements , we need to sort this array first .Once sorting is done , then it is straight forward to get kth element (smallest or largest element) using a[k-1]Here I am sharing code for smallest kth element of an array. For kth largest element is achieved through descending…

## Rearrange array alternatively

Consider an array (for example (1 3 2 4 6 12))of even number of elements like 2,4 or any number which is even . Need to arrange array in below order.First maximum First Minimum Second Max Second Min Third Max Third Min…. I have divided code into 4 parts.## Arrange elements in Descending order and create a blank array newlist of same length with value 0## arr=(1 3 2 4 6 12)len=${#arr[@]}k=0newlist=()lngthminusone=$(( $len -1))echo $lngthminusonewhile [ $k -lt $len ]doi=0value=0newlist+=$valuewhile [ $i -lt $lngthminusone ]doj=$(( $i + 1))if [ ${arr[i]}…

## All Sub Array having sum=0

In this blog, first we need to understand sub array definition and code to find sum of all Sub-Array in an Array. For that, please refer to this blog. Now, we need to find all Sub-Array whose Sum is equal to zero. Consider an array (1 -1 2 1 -3) and there are three sub-array whose sum is zero mentioned below.( 1 -1)(2 1 -3)(1 -1 2 1 -3) Below is the Shell Script which will give all sub-array whose sum is 0 . arr=(1 -1 2 1 -3) ##…

## Missing element in an Array

Consider a Array of N-1 elements which has values from 1 to N which means one of the element is always missing. So, In this Blog, sharing the code for the same.For Example a=( 4 3 2 5) , here number of elements is N-1 which means 4 and values ranges from 1 to N(which means 5) and hence we need to print that missing element or number is 1. arr=(4 3 2 5) ## Array of 4 elements(N-1)len_arr_1=$(( ${#arr[@]} +1)) ##Making length equal to N (5)len_arr=${#arr[@]} ## Traversing all…