Find Uncommon Characters in two Strings

In this Blog, Sharing code to find characters which are not common in the 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…

Remove all repeated Characters in a String

In this blog, sharing the code to remove consecutively repeated characters in a String. Consider a String “allabouttechnologies” . Here, l and t are repeated Characters. So, after applying logic, it will be like “alaboutechnologies“. str=”alllabouttechnologies” ##Sample Stringnewstr=” ##new Blank Stringn=${#str} ##length of a Sample StringPrevChar=${str:0:1} ##First Character of a Stringi=1while [[ $i -le $n ]] ##Looping Character by CharacterdoNextChar=${str:$i:1}if [[ $PrevChar == $NextChar ]] ##if two consecutive Character matches, it will remove or ignore the matched one and keep repeated Character oncethenecho “Ignore this Character: “$NextCharelsenewstr=${newstr}${PrevChar} ##merging only non-repeated…

Find first repeated character

In this blog, sharing the code to find the first repeated Character in a string. Consider a String “allabouttechnologies“, here l and t are the repeated Characters but l is the first repeated Character. str=”allabouttechnologies” ## Sample Stringn=${#str} ##Length of a StringPrevChar=${str:0:1} ## Set PrevChar as first character of a Stringi=1while [[ $i -lt $n ]] ## Looping through each character of a stringdoNextChar=${str:$i:1} ## Assigning NextChar equals to current Character in the Loopif [[ $PrevChar == $NextChar ]] ## Comparing consecutive Characters of a string in a loop, if…

Count Inversions in Array

Consider an Array (2 4 1 3 5). Inversions are those pairs where a[i]>a[j] and i<j . For above mentioned array, below are the pairs of Inversions. So, total Count Inversions are 3 for this Array.(2 1)(4 1)(4 3)Below is the code for the same using shell scripting. a=(2 4 1 3 5)len=${#a[@]}lngthminusone=$(( $len -1))i=0## This while loop will run inner loop for each element.while [ $i -lt $len ] doj=0## This while loop will compare one element with rest of the elements.while [ $j -lt $lngthminusone ] doj=$(( $j…

Check whether length of two array is same or not in Linux

In this blog, sharing code to check whether length of two arrays is same or not. arr=(4 3 2 1) ## First Array of 4 elementsarr_2=(4 3 1 10 1) ## Second Array of 5 elementslen_arr=${#arr[@]}len_arr_2=${#arr_2[@]}if [ $len_arr -eq $len_arr_2 ]thenecho “Length of both Array is same”fiif [ $len_arr -ne $len_arr_2 ]thenecho “Length of both Array is not same”fi