codeparrot/xlcost-text-to-code huggingface dataset:
Level order traversal line by line | Set 3 ( Using One Queue ) | A Binary Tree Node ; Prints level order traversal line by line using two queues . ; Create an empty queue for level order traversal ; Pushing root node into the queue . ; Pushing delimiter into the queue . ; Executing loop till queue becomes empty ; condition to check the occurence of next level ; Pushing left child current node ; Pushing right child current node ; Let us create binary tree shown above
class Node { constructor ( data ) { this . data = data ; this . left = null ; this . right = null ; } } function levelOrder ( root ) { if ( root == null ) return ; let q = [ ] ; q . push ( root ) ; q . push ( null ) ; while ( q . length != 0 ) { let curr = q . shift ( ) ; if ( curr == null ) { if ( q . length != 0 ) { q . push ( null ) ; document . write ( " " ) ; } } else { if ( curr . left != null ) q . push ( curr . left ) ; if ( curr . right != null ) q . push ( curr . right ) ; document . write ( curr . data + " " ) ; } } } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; levelOrder ( root ) ;
Convert given string so that it holds only distinct characters | Function to return the index of the character that has 0 occurrence starting from index i ; If current character has 0 occurrence ; If no character has 0 occurrence ; Function to return the modified string which consists of distinct characters ; String cannot consist of all distinct characters ; Count the occurrences for each of the character ; Index for the first character that hasn 't appeared in the string ; If current character appeared more than once then it has to be replaced with some character that hasn 't occurred yet ; Decrement current character 's occurrence by 1 ; Replace the character ; Update the new character ' s ▁ occurrence ▁ ▁ This ▁ step ▁ can ▁ also ▁ be ▁ skipped ▁ ▁ as ▁ we ' ll never encounter this character in the string because it has been added just now ; Find the next character that hasn 't occurred yet ; Return the modified string ; Driver code
function nextZero ( i , occurrences ) { while ( i < occurrences . length ) { if ( occurrences [ i ] == 0 ) return i ; i ++ ; } return - 1 ; } function getModifiedString ( str ) { let n = str . length ; if ( n > 26 ) return " " ; let ch = str . split ( ' ' ) ; let occurrences = new Array ( 26 ) ; occurrences . fill ( 0 ) ; let i ; for ( i = 0 ; i < n ; i ++ ) occurrences [ ch [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; let index = nextZero ( 0 , occurrences ) ; for ( i = 0 ; i < n ; i ++ ) { if ( occurrences [ ch [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] > 1 ) { occurrences [ ch [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] -- ; ch [ i ] = String . fromCharCode ( ' ' . charCodeAt ( ) + index ) ; occurrences [ index ] = 1 ; index = nextZero ( index + 1 , occurrences ) ; } } let s = ch . join ( " " ) ; return s ; } let str = " " ; document . write ( getModifiedString ( str ) ) ;
Sub | Function to return the count of sub - strings starting from startIndex that are also the prefixes of str ; Function to return the count of all possible sub - strings of str that are also the prefixes of str ; If current character is equal to the starting character of str ; Driver code ; Function Call
function subStringsStartingHere ( str , n , startIndex ) { var count = 0 , i = startIndex + 1 ; while ( i <= n ) { if ( str . startsWith ( str . substring ( startIndex , i ) ) ) { count ++ ; } else break ; i ++ ; } return count ; } function countSubStrings ( str , n ) { var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) count += subStringsStartingHere ( str , n , i ) ; } return count ; } var str = " " ; var n = str . length ; document . write ( countSubStrings ( str , n ) ) ;
Binary Search a String | Returns index of x if it is present in arr [ ] , else return - 1 ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; Driver method to test above
function binarySearch ( arr , x ) { let l = 0 , r = arr . length - 1 ; while ( l <= r ) { let m = l + Math . floor ( ( r - l ) / 2 ) ; let res = x . localeCompare ( arr [ m ] ) ; if ( res == 0 ) return m ; if ( res > 0 ) l = m + 1 ; else r = m - 1 ; } return - 1 ; } let arr = [ " " , " " , " " , " " ] ; let x = " " ; let result = binarySearch ( arr , x ) ; if ( result == - 1 ) document . write ( " " ) ; else document . write ( " " + " " + result + " " ) ;
Count characters in a string whose ASCII values are prime | JavaScript implementation of above approach ; Function to find prime characters in the String ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a Boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; initialize the value ; 0 and 1 are not primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Traverse all the characters ; Driver Code ; print required answer
const max_val = 257 ; function PrimeCharacters ( s ) { var prime = new Array ( max_val + 1 ) ; for ( var i = 0 ; i <= max_val ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( var p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] === true ) { for ( var i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } var count = 0 ; for ( var i = 0 ; i < s . length ; ++ i ) { if ( prime [ s [ i ] . charCodeAt ( 0 ) ] ) count ++ ; } return count ; } var S = " " ; document . write ( PrimeCharacters ( S ) ) ;
Length of largest subsequence consisting of a pair of alternating digits | Js program for the above approach Function to find the length of the largest subsequence consisting of a pair of alternating digits ; Function to find the length of the largest subsequence consisting of a pair of alternating digits ; Variable initialization ; Nested loops for iteration ; Check if i is not equal to j ; Initialize length as 0 ; Iterate from 0 till the size of the string ; Increment length ; Increment length ; Update maxi ; Check if maxi is not equal to 1 the print it otherwise print 0 ; Given string ; Function call
function largestSubsequence ( s ) { function largestSubsequence ( s ) { let maxi = 0 ; let prev1 ; for ( let i = 0 ; i < 10 ; i ++ ) { for ( let j = 0 ; j < 10 ; j ++ ) { if ( i != j ) { let len = 0 ; prev1 = String ( j ) ; for ( let k = 0 ; k < s . length ; k ++ ) { if ( s [ k ] == String ( i ) && prev1 == String ( j ) ) { prev1 = s [ k ] ; len ++ ; } else if ( s [ k ] == String ( j ) && prev1 == String ( i ) ) { prev1 = s [ k ] ; len ++ ; } } maxi = Math . max ( len , maxi ) ; } } } if ( maxi != 1 ) document . write ( maxi , ' ' ) ; else document . write ( 0 , ' ' ) ; } let s = " " ; largestSubsequence ( s ) ;
Students with maximum average score of three subjects | Function to find the list of students having maximum average score Driver code ; Variables to store average score of a student and maximum average score ; List to store names of students having maximum average score ; Traversing the file data ; finding average score of a student ; Clear the list and add name of student having current maximum average score in the list ; Printing the maximum average score and names of students having this maximum average score as per the order in the file . ; Driver code ; Number of elements in string array
function getStudentsList ( file , n ) { let avgScore ; let maxAvgScore = Number . MIN_SAFE_INTEGER ; let names = [ ] ; for ( let i = 0 ; i < n ; i += 4 ) { avgScore = Math . floor ( ( Number ( file [ i + 1 ] ) + Number ( file [ i + 2 ] ) + Number ( file [ i + 3 ] ) ) / 3 ) ; if ( avgScore > maxAvgScore ) { maxAvgScore = avgScore ; names = [ ] ; names . push ( file [ i ] ) ; } else if ( avgScore == maxAvgScore ) names . push ( file [ i ] ) ; } for ( let i = 0 ; i < names . length ; i ++ ) { document . write ( names [ i ] + " " ) ; } document . write ( maxAvgScore ) ; } let file = [ " " , " " , " " , " " , " " , " " , " " , " " ] ; let n = file . length ; getStudentsList ( file , n ) ;
Number of sub | JavaScript program to find the number of sub - Strings of s1 which are anagram of any sub - String of s2 ; This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of String pat [ ] in String txt [ ] ; countP [ ] : Store count of all characters of pattern countTW [ ] : Store count of current window of text ; Traverse through remaining characters of pattern ; Compare counts of current window of text with counts of pattern [ ] ; cout << pat << " ▁ " << txt << " ▁ " ; ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Function to return the number of sub - Strings of s1 that are anagrams of any sub - String of s2 ; initializing variables ; outer loop for picking starting point ; loop for different length of subStrings ; If s2 has any subString which is anagram of s1 . substr ( i , len ) ; increment the count ; Driver code
let MAX_LEN = 1005 ; let MAX_CHAR = 26 ; let ALL_CHARS = 256 ; function compare ( arr1 , arr2 ) { for ( let i = 0 ; i < ALL_CHARS ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } function search ( pat , txt ) { let M = pat . length ; let N = txt . length ; let i ; let countP = new Array ( ALL_CHARS ) ; countP . fill ( 0 ) ; let countTW = new Array ( ALL_CHARS ) ; countTW . fill ( 0 ) ; for ( i = 0 ; i < M ; i ++ ) { countP [ pat [ i ] . charCodeAt ( ) ] ++ ; countTW [ txt [ i ] . charCodeAt ( ) ] ++ ; } for ( i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) { return true ; } countTW [ txt [ i ] . charCodeAt ( ) ] ++ ; countTW [ txt [ i - M ] . charCodeAt ( ) ] -- ; } if ( compare ( countP , countTW ) ) return true ; return false ; } function calculatesubString ( s1 , s2 , n ) { let count = 0 , j = 0 , x = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let len = 1 ; len <= n - i ; len ++ ) { if ( search ( s1 . substring ( i , i + len ) , s2 ) ) { count = count + 1 ; } } } return count ; } let str1 = " " ; let str2 = " " ; let len = str1 . length ; document . write ( calculatesubString ( str1 , str2 , len ) ) ;
Sum of the alphabetical values of the characters of a string | Function to find string score ; Driver code
function strScore ( str , s , n ) { var score = 0 , index = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( str [ i ] == s ) { for ( j = 0 ; j < s . length ; j ++ ) { score += s . charAt ( j ) . charCodeAt ( 0 ) - ( ' ' ) . charCodeAt ( 0 ) + 1 ; } index = i + 1 ; break ; } } score = score * index ; return score ; } str = [ " " , " " , " " , " " , " " ] ; s = " " ; var n = str . length ; var score = strScore ( str , s , n ) ; document . write ( score ) ;
Union | Naive implementation of find ; Naive implementation of union ( )
function find ( parent , i ) { if ( parent [ i ] == - 1 ) return i ; return find ( parent , parent [ i ] ) ; } function Union ( parent , x , y ) { let xset = find ( parent , x ) ; let yset = find ( parent , y ) ; parent [ xset ] = yset ; }
Get K | Utility function ; Function to return the K - th letter from new String . ; finding size = length of new string S ' ; get the K - th letter ; Driver program
function isAlphaNumeric ( str ) { var code , i , len ; for ( i = 0 , len = str . length ; i < len ; i ++ ) { code = str . charCodeAt ( i ) ; return false ; } } return true ; } ; function K_thletter ( S , K ) { var N = S . length ; var size = 0 ; for ( var i = 0 ; i < N ; ++ i ) { if ( Number . isInteger ( parseInt ( S [ i ] ) ) ) size = size * ( S [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; else size += 1 ; } for ( var i = N - 1 ; i >= 0 ; -- i ) { K %= size ; if ( K == 0 && isAlphaNumeric ( S [ i ] ) ) return " " + S [ i ] ; if ( Number . isInteger ( parseInt ( S [ i ] ) ) ) size = size / ( S [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; else size -= 1 ; } } var S = " " ; var K = 15 ; document . write ( K_thletter ( S , K ) ) ;
Minimum number of Parentheses to be added to make it valid | Function to return required minimum number ; maintain balance of string ; It is guaranteed bal >= - 1 ; Driver code ; Function to print required answer
function minParentheses ( p ) { var bal = 0 ; var ans = 0 ; for ( var i = 0 ; i < p . length ; ++ i ) { bal += p [ i ] == ' ' ? 1 : - 1 ; if ( bal == - 1 ) { ans += 1 ; bal += 1 ; } } return bal + ans ; } var p = " " ; document . write ( minParentheses ( p ) ) ;
Check if suffix and prefix of a string are palindromes | Function to check whether the string is a palindrome ; reverse the string to compare with the original string ; check if both are same ; Function to check whether the string has prefix and suffix substrings of length greater than 1 which are palindromes . ; check all prefix substrings ; check if the prefix substring is a palindrome ; If we did not find any palindrome prefix of length greater than 1. ; check all suffix substrings , as the string is reversed now ; check if the suffix substring is a palindrome ; If we did not find a suffix ; Driver code
function isPalindrome ( r ) { var p = r ; p . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; return ( r == p ) ; } function CheckStr ( s ) { var l = s . length ; var i ; for ( i = 2 ; i <= l ; i ++ ) { if ( isPalindrome ( s . substring ( 0 , i ) ) ) break ; } if ( i == ( l + 1 ) ) return false ; i = 2 ; for ( i = 2 ; i <= l ; i ++ ) { if ( isPalindrome ( s . substring ( l - i , l ) ) ) return true ; } return false ; } var s = " " ; if ( CheckStr ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Rotations of a Binary String with Odd Value | function to calculate total odd decimal equivalent ; Driver code
function oddEquivalent ( s , n ) { var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) count ++ ; } return count ; } var s = " " ; var n = s . length ; document . write ( oddEquivalent ( s , n ) ) ;
Lexicographically smallest and largest substring of size k | JavaScript program to find lexicographically largest and smallest substrings of size k . ; Initialize min and max as first substring of size k ; Consider all remaining substrings . We consider every substring ending with index i . ; Print result . ; Driver Code
function getSmallestAndLargest ( s , k ) { var currStr = s . substring ( 0 , k ) ; var lexMin = currStr ; var lexMax = currStr ; for ( var i = k ; i < s . length ; i ++ ) { currStr = currStr . substring ( 1 , k ) + s [ i ] ; if ( lexMax < currStr ) lexMax = currStr ; if ( lexMin > currStr ) lexMin = currStr ; } document . write ( lexMin + " " ) ; document . write ( lexMax + " " ) ; } var str = " " ; var k = 3 ; getSmallestAndLargest ( str , k ) ;
Count of strings that can be formed from another string using each character at | Function to find the number of str2 that can be formed using characters of str1 ; iterate and mark the frequencies of all characters in str1 ; find the minimum frequency of every character in str1 ; Driver Code
function findNumberOfTimes ( str1 , str2 ) { let freq = new Array ( 26 ) ; let freq2 = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { freq [ i ] = 0 ; freq2 [ i ] = 0 ; } let l1 = str1 . length ; for ( let i = 0 ; i < l1 ; i ++ ) { freq [ str1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; } let l2 = str2 . length ; for ( let i = 0 ; i < l2 ; i ++ ) { freq2 [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; } let count = Number . MAX_VALUE ; for ( let i = 0 ; i < l2 ; i ++ ) { if ( freq2 [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] != 0 ) count = Math . min ( count , freq [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] / freq2 [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ) ; } return count ; } let str1 = " " ; let str2 = " " ; document . write ( findNumberOfTimes ( str1 , str2 ) ) ;
String transformation using XOR and OR | function to check if conversion is possible or not ; if lengths are different ; iterate to check if both strings have 1 ; to check if there is even one 1 in string s1 ; to check if there is even one 1 in string s2 ; if both strings have only '0' ; if both string do not have a '1' . ; Driver code
function solve ( s1 , s2 ) { let flag1 = 0 , flag2 = 0 ; if ( s1 . length != s2 . length ) return false ; let l = s1 . length ; for ( let i = 0 ; i < l ; i ++ ) { if ( s1 [ i ] == ' ' ) flag1 = 1 ; if ( s2 [ i ] == ' ' ) flag2 = 1 ; if ( flag1 && flag2 ) return true ; } if ( ! flag1 && ! flag2 ) return true ; return false ; } let s1 = " " ; let s2 = " " ; if ( solve ( s1 , s2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Smallest alphabet greater than a given character | Returns the smallest character from the given set of letters that is greater than K ; Take the first element as l and the rightmost element as r ; if this while condition does not satisfy simply return the first element . ; Return the smallest element ; Driver Code ; Function call
function nextGreatestAlphabet ( alphabets , K ) { var l = 0 , r = alphabets . length - 1 ; var ans = - 1 ; while ( l <= r ) { var mid = ( l + r ) / 2 ; if ( alphabets [ mid ] > K ) { ans = mid ; r = mid - 1 ; } else l = mid + 1 ; } return alphabets [ ans ] ; } var letters = [ " " , " " , " " ] ; var K = " " ; document . write ( nextGreatestAlphabet ( letters , K ) ) ;
Longest substring of 0 s in a string formed by k concatenations | Function to calculate maximum length of substring containing only zero ; loop to first calculate longest substring in string ; if all elements in string are '0' ; Else , find size of prefix and suffix containing only zeroes ; Calculate prefix containing only zeroes ; Calculate suffix containing only zeroes ; if k <= 1 then there is no need to take prefix + suffix into account ; Drivers code
function subzero ( s , k ) { let ans = 0 , curr = 0 ; let len = s . length ; let str = s . split ( " " ) ; for ( let i = 0 ; i < len ; ++ i ) { if ( str [ i ] == ' ' ) curr ++ ; else curr = 0 ; ans = Math . max ( ans , curr ) ; } if ( ans == len ) return len * k ; else { let pre = 0 , suff = 0 ; for ( let i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ' ) pre ++ ; else break ; } for ( let i = len - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' ) suff ++ ; else break ; } if ( k > 1 ) ans = Math . max ( ans , pre + suff ) ; return ans ; } } let str = " " ; let k = 5 ; document . write ( subzero ( str , k ) ) ;
Find nth term of the Dragon Curve Sequence | function to generate the nth term ; first term ; generating each term of the sequence ; loop to generate the ith term ; add character from the original string ; add alternate 0 and 1 in between ; if previous added term was '0' then add '1' ; now current term becomes previous term ; if previous added term was '1' , then add '0' ; now current term becomes previous term ; s becomes the ith term of the sequence ; Taking inputs ; generate nth term of dragon curve sequence ; Printing output
function Dragon_Curve_Sequence ( n ) { let s = " " ; for ( let i = 2 ; i <= n ; i ++ ) { let temp = " " ; let prev = ' ' ; let zero = ' ' ; let one = ' ' ; for ( let j = 0 ; j < s . length ; j ++ ) { temp = temp + s [ j ] ; if ( prev == ' ' ) { temp += one ; prev = one ; } else { temp += zero ; prev = zero ; } } s = temp ; } return s ; } let n = 4 ; let s = Dragon_Curve_Sequence ( n ) ; document . write ( s + " " ) ;
Number of substrings of a string | JavaScript program to count number of substrings of a string ; Driver code
function countNonEmptySubstr ( str ) { let n = str . length ; return n * ( n + 1 ) / 2 ; } let s = " " ; document . write ( countNonEmptySubstr ( s ) ) ;
Count substrings with each character occurring at most k times | Javascript program to count number of substrings in which each character has count less than or equal to k . ; variable to store count of substrings . ; array to store count of each character . ; increment character count ; check only the count of current character because only the count of this character is changed . The ending point is incremented to current position only if all other characters have count at most k and hence their count is not checked . If count is less than k , then increase ans by 1. ; if count is less than k , then break as subsequent substrings for this starting point will also have count greater than k and hence are reduntant to check . ; return the final count of substrings . ; Driver code
function findSubstrings ( s , k ) { var ans = 0 ; var cnt = Array ( 26 ) ; var i , j , n = s . length ; for ( i = 0 ; i < n ; i ++ ) { cnt = Array ( 26 ) . fill ( 0 ) ; for ( j = i ; j < n ; j ++ ) { cnt [ ( s [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ] ++ ; if ( cnt [ ( s [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ] <= k ) ans ++ ; else break ; } } return ans ; } var S = " " ; var k = 2 ; document . write ( findSubstrings ( S , k ) ) ;
Check if characters of one string can be swapped to form other | Javascript program to check if characters of one string can be swapped to form other ; if length is not same print no ; Count frequencies of character in first string . ; iterate through the second string decrement counts of characters in second string ; Since lengths are same , some value would definitely become negative if result is false . ; Driver Code
let MAX = 26 ; function targetstring ( str1 , str2 ) { let l1 = str1 . length ; let l2 = str2 . length ; if ( l1 != l2 ) return false ; let map = new Array ( MAX ) ; for ( let i = 0 ; i < map . length ; i ++ ) { map [ i ] = 0 ; } for ( let i = 0 ; i < l1 ; i ++ ) map [ str1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i < l2 ; i ++ ) { map [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; if ( map [ str2 [ i ] - ' ' . charCodeAt ( 0 ) ] < 0 ) return false ; } return true ; } let str1 = " " ; let str2 = " " ; if ( targetstring ( str1 , str2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the Number which contain the digit d | Returns true if d is present as digit in number x . ; Breal loop if d is present as digit ; If loop broke ; Function to display the values ; Check all numbers one by one ; Checking for digit ; Driver code
function isDigitPresent ( x , d ) { while ( x > 0 ) { if ( x % 10 == d ) break ; x = x / 10 ; } return ( x > 0 ) ; } function printNumbers ( n , d ) { for ( let i = 0 ; i <= n ; i ++ ) if ( i == d || isDigitPresent ( i , d ) ) document . write ( i + " " ) ; } let n = 47 , d = 7 ; printNumbers ( n , d ) ;
Find one extra character in a string | JavaScript program to find extra character in one string ; store string values in map ; store second string in map with frequency ; store first string in map with frequency ; if the frequency is 1 then this character is which is added extra ; given string ; find Extra Character
function findExtraCharcter ( strA , strB ) { let m1 = new Array ( 256 ) ; for ( let i = 0 ; i < 256 ; i ++ ) m1 [ i ] = 0 ; for ( let i = 0 ; i < strB . length ; i ++ ) m1 [ strB [ i ] . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i < strA . length ; i ++ ) m1 [ strA [ i ] . charCodeAt ( 0 ) ] -- ; for ( let i = 0 ; i < m1 . length ; i ++ ) { if ( m1 [ i ] == 1 ) return String . fromCharCode ( i ) ; } return Number . MIN_VALUE ; } let strA = " " ; let strB = " " ; document . write ( findExtraCharcter ( strA . split ( " " ) , strB . split ( " " ) ) ) ;
Find one extra character in a string | Javascript program to find extra character in one string ; result store the result ; traverse string A till end and xor with res ; xor with res ; traverse string B till end and xor with res ; xor with res ; print result at the end ; given string
function findExtraCharcter ( strA , strB ) { let res = 0 ; for ( let i = 0 ; i < strA . length ; i ++ ) { res ^= strA . charCodeAt ( i ) ; } for ( let i = 0 ; i < strB . length ; i ++ ) { res ^= strB . charCodeAt ( i ) ; } return res ; } let strA = " " ; let strB = " " ; document . write ( String . fromCharCode ( findExtraCharcter ( strA , strB ) ) ) ;