codeparrot/xlcost-text-to-code huggingface dataset:
QuickSelect ( A Simple Iterative Implementation ) | Standard Lomuto partition function ; Implementation of QuickSelect ; Partition a [ left . . right ] around a pivot and find the position of the pivot ; If pivot itself is the k - th smallest element ; If there are more than k - 1 elements on left of pivot , then k - th smallest must be on left side . ; Else k - th smallest is on right side . ; Driver Code
function partition ( arr , low , high ) { let temp ; let pivot = arr [ high ] ; let i = ( low - 1 ) ; for ( let j = low ; j <= high - 1 ; j ++ ) { if ( arr [ j ] <= pivot ) { i ++ ; temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } temp = arr [ i + 1 ] ; arr [ i + 1 ] = arr [ high ] ; arr [ high ] = temp ; return ( i + 1 ) ; } function kthSmallest ( a , left , right , k ) { while ( left <= right ) { let pivotIndex = partition ( a , left , right ) ; if ( pivotIndex == k - 1 ) return a [ pivotIndex ] ; else if ( pivotIndex > k - 1 ) right = pivotIndex - 1 ; else left = pivotIndex + 1 ; } return - 1 ; } let arr = [ 10 , 4 , 5 , 8 , 11 , 6 , 26 ] ; let n = arr . length ; let k = 5 ; document . write ( " " + kthSmallest ( arr , 0 , n - 1 , k ) ) ;
Pair with largest sum which is less than K in the array | Function to find pair with largest sum which is less than K in the array ; To store the break point ; Sort the given array ; Find the break point ; No need to look beyond i 'th index ; Find the required pair ; Print the required answer ; Driver code ; Function call
function Max_Sum ( arr , n , k ) { let p = n ; arr . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= k ) { p = i ; break ; } } let maxsum = 0 , a = 0 , b = 0 ; for ( let i = 0 ; i < p ; i ++ ) { for ( let j = i + 1 ; j < p ; j ++ ) { if ( arr [ i ] + arr [ j ] < k && arr [ i ] + arr [ j ] > maxsum ) { maxsum = arr [ i ] + arr [ j ] ; a = arr [ i ] ; b = arr [ j ] ; } } } document . write ( a + " " + b + " " ) ; } let arr = [ 5 , 20 , 110 , 100 , 10 ] ; let k = 85 ; let n = arr . length ; Max_Sum ( arr , n , k ) ;
Maximum length palindromic substring such that it starts and ends with given char | Function that returns true if str [ i ... j ] is a palindrome ; Function to return the length of the longest palindromic sub - string such that it starts and ends with the character ch ; If current character is a valid starting index ; Instead of finding the ending index from the beginning , find the index from the end This is because if the current sub - string is a palindrome then there is no need to check the sub - strings of smaller length and we can skip to the next iteration of the outer loop ; If current character is a valid ending index ; If str [ i ... j ] is a palindrome then update the length of the maximum palindrome so far ; Driver code
function isPalindrome ( str , i , j ) { while ( i < j ) { if ( str [ i ] !== str [ j ] ) { return false ; } i ++ ; j -- ; } return true ; } function maxLenPalindrome ( str , n , ch ) { var maxLen = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] === ch ) { for ( var j = n - 1 ; j >= i ; j -- ) { if ( str [ j ] === ch ) { if ( isPalindrome ( str , i , j ) ) { maxLen = Math . max ( maxLen , j - i + 1 ) ; break ; } } } } } return maxLen ; } var str = " " ; var n = str . length ; var ch = " " ; document . write ( maxLenPalindrome ( str , n , ch ) ) ;
Longest sub | Javascript implementation of the approach ; Function to find the starting and the ending index of the sub - array with equal number of alphabets and numeric digits ; If its an alphabet ; Else its a number ; Pick a starting point as i ; Consider all sub - arrays starting from i ; If this is a 0 sum sub - array then compare it with maximum size sub - array calculated so far ; If no valid sub - array found ; Driver code
function isalpha ( input_char ) { if ( ( input_char >= 65 && input_char <= 90 ) || ( input_char >= 97 && input_char <= 122 ) ) return true ; return false ; } function findSubArray ( arr , n ) { let sum = 0 ; let maxsize = - 1 , startindex = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isalpha ( arr [ i ] . charCodeAt ( ) ) ) { arr [ i ] = 0 ; } else { arr [ i ] = 1 ; } } for ( let i = 0 ; i < n - 1 ; i ++ ) { sum = ( arr [ i ] == 0 ) ? - 1 : 1 ; for ( let j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] == 0 ) sum += - 1 ; else sum += 1 ; if ( sum == 0 && maxsize < j - i + 1 ) { maxsize = j - i + 1 ; startindex = i ; } } } if ( maxsize == - 1 ) document . write ( maxsize + " " ) ; else document . write ( startindex + " " + ( startindex + maxsize - 1 ) + " " ) ; } let arr = [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ; let size = arr . length ; findSubArray ( arr , size ) ;
Check if given string can be formed by two other strings or their permutations | Javascript implementation of the approach ; Function to sort the given string using counting sort ; Array to store the count of each character ; Insert characters in the string in increasing order ; Function that returns true if str can be generated from any permutation of the two strings selected from the given vector ; Sort the given string ; Select two strings at a time from given vector ; Get the concatenated string ; Sort the resultant string ; If the resultant string is equal to the given string str ; No valid pair found ; Driver code
let MAX = 26 ; function countingsort ( s ) { let count = new Array ( MAX ) ; count . fill ( 0 ) ; for ( let i = 0 ; i < s . length ; i ++ ) { count [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; } let index = 0 ; for ( let i = 0 ; i < MAX ; i ++ ) { let j = 0 ; while ( j < count [ i ] ) { s [ index ++ ] = String . fromCharCode ( i + ' ' . charCodeAt ( ) ) ; j ++ ; } } return s . join ( " " ) ; } function isPossible ( v , str ) { str = countingsort ( str . split ( ' ' ) ) ; for ( let i = 0 ; i < v . length - 1 ; i ++ ) { for ( let j = i + 1 ; j < v . length ; j ++ ) { let temp = v [ i ] + v [ j ] ; temp = countingsort ( temp . split ( ' ' ) ) ; if ( temp == str ) { return true ; } } } return false ; } let str = " " ; let v = [ " " , " " , " " , " " , " " ] ; if ( isPossible ( v , str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Queries to find the first non | Maximum distinct characters possible ; To store the frequency of the characters ; Function to pre - calculate the frequency array ; Only the first character has frequency 1 till index 0 ; Starting from the second character of the string ; For every possible character ; Current character under consideration ; If it is equal to the character at the current index ; Function to return the frequency of the given character in the sub - string str [ l ... r ] ; Function to return the first non - repeating character in range [ l . . r ] ; Starting from the first character ; Current character ; If frequency of the current character is 1 then return the character ; All the characters of the sub - string are repeating ; Driver code ; Pre - calculate the frequency array
let MAX = 256 ; let freq ; function preCalculate ( str , n ) { freq [ str [ 0 ] . charCodeAt ( ) ] [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { let ch = str [ i ] ; for ( let j = 0 ; j < MAX ; j ++ ) { let charToUpdate = String . fromCharCode ( j ) ; if ( charToUpdate == ch ) freq [ j ] [ i ] = freq [ j ] [ i - 1 ] + 1 ; else freq [ j ] [ i ] = freq [ j ] [ i - 1 ] ; } } } function getFrequency ( ch , l , r ) { if ( l == 0 ) return freq [ ch . charCodeAt ( ) ] [ r ] ; else return ( freq [ ch . charCodeAt ( ) ] [ r ] - freq [ ch . charCodeAt ( ) ] [ l - 1 ] ) ; } function firstNonRepeating ( str , n , l , r ) { for ( let i = l ; i < r ; i ++ ) { let ch = str [ i ] ; if ( getFrequency ( ch , l , r ) == 1 ) return ( " " + ch ) ; } return " " ; } let str = " " ; let n = str . length ; let queries = [ [ 0 , 3 ] , [ 2 , 3 ] , [ 5 , 12 ] ] ; let q = queries . length ; freq = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { freq [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { freq [ i ] [ j ] = 0 ; } } preCalculate ( str , n ) ; for ( let i = 0 ; i < q ; i ++ ) { document . write ( firstNonRepeating ( str , n , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) + " " ) ; }
Length of the largest substring which have character with frequency greater than or equal to half of the substring | Function to return the length of the longest sub string having frequency of a character greater than half of the length of the sub string ; for each of the character ' a ' to ' z ' ; finding frequency prefix array of the character ; Finding the r [ ] and l [ ] arrays . ; for each j from 0 to n ; Finding the lower bound of i . ; storing the maximum value of i - j + 1 ; clearing all the vector so that it can be used for other characters . ; Driver Code
function maxLength ( s , n ) { var ans = - 2147483648 ; var A = [ ] ; var L = [ ] ; var R = [ ] ; var freq = new Array ( n + 5 ) . fill ( 0 ) ; for ( var i = 0 ; i < 26 ; i ++ ) { var count = 0 ; for ( var j = 0 ; j < n ; j ++ ) { if ( s [ j ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) === i ) count ++ ; freq [ j ] = count ; } for ( var j = 1 ; j < n ; j ++ ) { L . push ( 2 * freq [ j - 1 ] - j ) ; R . push ( 2 * freq [ j ] - j ) ; } var max_len = - 2147483648 ; var min_val = 2147483648 ; for ( var j = 0 ; j < L . length ; j ++ ) { min_val = Math . min ( min_val , L [ j ] ) ; A . push ( min_val ) ; var l = 0 , r = j ; while ( l <= r ) { var mid = ( l + r ) >> 1 ; if ( A [ mid ] <= R [ j ] ) { max_len = Math . max ( max_len , j - mid + 1 ) ; r = mid - 1 ; } else { l = mid + 1 ; } } } ans = Math . max ( ans , max_len ) ; A = [ ] ; R = [ ] ; L = [ ] ; } return ans ; } var s = " " ; var n = s . length ; document . write ( maxLength ( s , n ) ) ;
Repeated Character Whose First Appearance is Leftmost | Javascript program to find first repeating character ; The function returns index of the first repeating character in a string . If all characters are repeating then returns - 1 ; Mark all characters as not visited ; Traverse from right and update res as soon as we see a visited character . ; Driver Code
let NO_OF_CHARS = 256 ; function firstRepeating ( str ) { let visited = new Array ( NO_OF_CHARS ) ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) visited [ i ] = false ; let res = - 1 ; for ( let i = str . length - 1 ; i >= 0 ; i -- ) { if ( visited [ str [ i ] . charCodeAt ( ) ] == false ) visited [ str [ i ] . charCodeAt ( ) ] = true ; else res = i ; } return res ; } let str = " " ; let index = firstRepeating ( str ) ; if ( index == - 1 ) document . write ( " " + " " ) ; else document . write ( " " + " " + str [ index ] ) ;
Find maximum sum taking every Kth element in the array | Function to return the maximum sum for every possible sequence such that a [ i ] + a [ i + k ] + a [ i + 2 k ] + ... + a [ i + qk ] is maximized ; Initialize the maximum with the smallest value ; Initialize the sum array with zero ; Iterate from the right ; Update the sum starting at the current element ; Update the maximum so far ; Driver code
function maxSum ( arr , n , K ) { var maximum = - 1000000000 ; var sum = Array ( n ) . fill ( 0 ) ; for ( var i = n - 1 ; i >= 0 ; i -- ) { if ( i + K < n ) sum [ i ] = sum [ i + K ] + arr [ i ] ; else sum [ i ] = arr [ i ] ; maximum = Math . max ( maximum , sum [ i ] ) ; } return maximum ; } var arr = [ 3 , 6 , 4 , 7 , 2 ] ; var n = arr . length ; var K = 2 ; document . write ( maxSum ( arr , n , K ) ) ;
Count common characters in two strings | Function to return the count of valid indices pairs ; To store the frequencies of characters of string s1 and s2 ; To store the count of valid pairs ; Update the frequencies of the characters of string s1 ; Update the frequencies of the characters of string s2 ; Find the count of valid pairs ; Driver code
function countPairs ( s1 , n1 , s2 , n2 ) { let freq1 = new Array ( 26 ) ; let freq2 = new Array ( 26 ) ; freq1 . fill ( 0 ) ; freq2 . fill ( 0 ) ; let i , count = 0 ; for ( i = 0 ; i < n1 ; i ++ ) freq1 [ s1 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; for ( i = 0 ; i < n2 ; i ++ ) freq2 [ s2 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; for ( i = 0 ; i < 26 ; i ++ ) count += ( Math . min ( freq1 [ i ] , freq2 [ i ] ) ) ; return count ; } let s1 = " " , s2 = " " ; let n1 = s1 . length , n2 = s2 . length ; document . write ( countPairs ( s1 , n1 , s2 , n2 ) ) ;
Find a distinct pair ( x , y ) in given range such that x divides y | JavaScript implementation of the approach ; Driver code
function findpair ( l , r ) { let c = 0 ; for ( let i = l ; i <= r ; i ++ ) { for ( let j = i + 1 ; j <= r ; j ++ ) { if ( j % i == 0 && j != i ) { document . write ( i + " " + j + " " ) ; c = 1 ; break ; } } if ( c == 1 ) break ; } } let l = 1 , r = 10 ; findpair ( l , r ) ;
Check if the given array can be reduced to zeros with the given operation performed given number of times | Function that returns true if the array can be reduced to 0 s with the given operation performed given number of times ; Set to store unique elements ; Add every element of the array to the set ; Count of all the unique elements in the array ; Driver code
function check ( arr , N , K ) { var unique = new Set ( ) ; for ( var i = 0 ; i < N ; i ++ ) unique . add ( arr [ i ] ) ; if ( unique . size == K ) return true ; return false ; } var arr = [ 1 , 1 , 2 , 3 ] ; var N = arr . length ; var K = 3 ; if ( check ( arr , N , K ) ) document . write ( " " ) ; else document . write ( " " ) ;
Print all the sum pairs which occur maximum number of times | Function to find the sum pairs that occur the most ; Hash - table ; Keep a count of sum pairs ; Variables to store maximum occurrence ; Iterate in the hash table ; Print all sum pair which occur maximum number of times ; Driver code
function findSumPairs ( a , n ) { let mpp = new Map ( ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( mpp [ a [ i ] + a [ j ] ] ) mpp [ a [ i ] + a [ j ] ] ++ ; else mpp [ a [ i ] + a [ j ] ] = 1 ; } } let occur = 0 ; for ( var it in mpp ) { if ( mpp [ it ] > occur ) { occur = mpp [ it ] ; } } for ( var it in mpp ) { if ( mpp [ it ] == occur ) document . write ( it , ' ' ) ; } } let a = [ 1 , 8 , 3 , 11 , 4 , 9 , 2 , 7 ] ; let len = a . length ; findSumPairs ( a , len ) ;
Minimum index i such that all the elements from index i to given index are equal | Function to return the minimum required index ; Start from arr [ pos - 1 ] ; All elements are equal from arr [ i + 1 ] to arr [ pos ] ; Driver code ; Function Call
function minIndex ( arr , n , pos ) { var num = arr [ pos ] ; var i = pos - 1 ; while ( i >= 0 ) { if ( arr [ i ] != num ) break ; i -- ; } return i + 1 ; } var arr = [ 2 , 1 , 1 , 1 , 5 , 2 ] ; var n = arr . length ; var pos = 4 ; document . write ( minIndex ( arr , n , pos ) ) ;
Minimum index i such that all the elements from index i to given index are equal | Function to return the minimum required index ; Short - circuit more comparisions as found the border point ; For cases were high = low + 1 and arr [ high ] will match with arr [ pos ] but not arr [ low ] or arr [ mid ] . In such iteration the if condition will satisfy and loop will break post that low will be updated . Hence i will not povar to the correct index . ; Driver code
function minIndex ( arr , pos ) { var low = 0 ; var high = pos ; var i = pos ; while ( low < high ) { var mid = parseInt ( ( low + high ) / 2 ) ; if ( arr [ mid ] != arr [ pos ] ) { low = mid + 1 ; } else { high = mid - 1 ; i = mid ; if ( mid > 0 && arr [ mid - 1 ] != arr [ pos ] ) { break ; } } } return arr [ low ] == arr [ pos ] ? low : i ; } var arr = [ 2 , 1 , 1 , 1 , 5 , 2 ] ;
Count of strings that become equal to one of the two strings after one removal | Function to return the count of the required strings ; Searching index after longest common prefix ends ; Searching index before longest common suffix ends ; If str1 = str2 ; If only 1 character is different in both the strings ; Checking remaining part of string for equality ; Searching in right of string h ( g to h ) ; Driver code
function findAnswer ( str1 , str2 , n ) { var l = 0 , r = 0 ; var ans = 2 ; for ( i = 0 ; i < n ; ++ i ) { if ( str1 . charAt ( i ) != str2 . charAt ( i ) ) { l = i ; break ; } } for ( i = n - 1 ; i >= 0 ; i -- ) { if ( str1 . charAt ( i ) != str2 . charAt ( i ) ) { r = i ; break ; } } if ( r < l ) return 26 * ( n + 1 ) ; else if ( l == r ) return ans ; else { for ( i = l + 1 ; i <= r ; i ++ ) { if ( str1 . charAt ( i ) != str2 . charAt ( i - 1 ) ) { ans -- ; break ; } } for ( i = l + 1 ; i <= r ; i ++ ) { if ( str1 . charAt ( i - 1 ) != str2 . charAt ( i ) ) { ans -- ; break ; } } return ans ; } } var str1 = " " , str2 = " " ; var n = str1 . length ; document . write ( findAnswer ( str1 , str2 , n ) ) ;
Minimize the maximum minimum difference after one removal from array | Function to return the minimum required difference ; Sort the given array ; When minimum element is removed ; When maximum element is removed ; Return the minimum of diff1 and diff2 ; Driver Code
function findMinDifference ( arr , n ) { arr . sort ( ) ; let diff1 = arr [ n - 1 ] - arr [ 1 ] ; let diff2 = arr [ n - 2 ] - arr [ 0 ] ; return Math . min ( diff1 , diff2 ) ; } let arr = [ 1 , 2 , 4 , 3 , 4 ] ; let n = arr . length ; document . write ( findMinDifference ( arr , n ) ) ;
Minimize the maximum minimum difference after one removal from array | Function to return the minimum required difference ; If current element is greater than max ; max will become secondMax ; Update the max ; If current element is greater than secondMax but smaller than max ; Update the secondMax ; If current element is smaller than min ; min will become secondMin ; Update the min ; If current element is smaller than secondMin but greater than min ; Update the secondMin ; Minimum of the two possible differences ; Driver code
function findMinDifference ( arr , n ) { let min__ , secondMin , max__ , secondMax ; min__ = secondMax = ( arr [ 0 ] < arr [ 1 ] ) ? arr [ 0 ] : arr [ 1 ] ; max__ = secondMin = ( arr [ 0 ] < arr [ 1 ] ) ? arr [ 1 ] : arr [ 0 ] ; for ( let i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > max__ ) { secondMax = max__ ; max__ = arr [ i ] ; } else if ( arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } else if ( arr [ i ] < min__ ) { secondMin = min__ ; min__ = arr [ i ] ; } else if ( arr [ i ] < secondMin ) { secondMin = arr [ i ] ; } } let diff = Math . min ( max__ - secondMin , secondMax - min__ ) ; return diff ; } let arr = [ 1 , 2 , 4 , 3 , 4 ] ; let n = arr . length ; document . write ( findMinDifference ( arr , n ) ) ;
Integers from the range that are composed of a single distinct digit | Boolean function to check distinct digits of a number ; Take last digit ; Check if all other digits are same as last digit ; Remove last digit ; Function to return the count of integers that are composed of a single distinct digit only ; If i has single distinct digit ; Driver code
function checkDistinct ( x ) { var last = x % 10 ; while ( x > 0 ) { if ( x % 10 != last ) return false ; x = parseInt ( x / 10 ) ; } return true ; } function findCount ( L , R ) { var count = 0 ; for ( i = L ; i <= R ; i ++ ) { if ( checkDistinct ( i ) ) count += 1 ; } return count ; } var L = 10 , R = 50 ; document . write ( findCount ( L , R ) ) ;
Smallest Pair Sum in an array | Function to return the sum of the minimum pair from the array ; If found new minimum ; Minimum now becomes second minimum ; Update minimum ; If current element is > min and < secondMin ; Update secondMin ; Return the sum of the minimum pair ; Driver code
function smallest_pair ( a , n ) { let min = Number . MAX_VALUE , secondMin = Number . MAX_VALUE ; for ( let j = 0 ; j < n ; j ++ ) { if ( a [ j ] < min ) { secondMin = min ; min = a [ j ] ; } else if ( ( a [ j ] < secondMin ) && a [ j ] != min ) secondMin = a [ j ] ; } return ( secondMin + min ) ; } let arr = [ 1 , 2 , 3 ] ; let n = arr . length ; document . write ( smallest_pair ( arr , n ) ) ;
Longest subarray with elements divisible by k | function to find longest subarray ; this will contain length of longest subarray found ;
function longestsubarray ( arr , n , k ) { let current_count = 0 ; let max_count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) current_count ++ ; else current_count = 0 ; max_count = Math . max ( current_count , max_count ) ; } return max_count ; } let arr = [ 2 , 5 , 11 , 32 , 64 , 88 ] ; let n = arr . length ; let k = 8 ; document . write ( longestsubarray ( arr , n , k ) ) ;
Remove elements that appear strictly less than k times | JavaScript program to remove the elements which appear strictly less than k times from the array . ; Hash map which will store the frequency of the elements of the array . ; Incrementing the frequency of the element by 1. ; Print the element which appear more than or equal to k times . ; Driver code
function removeElements ( arr , n , k ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( ! mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , 1 ) ; else { let x = mp . get ( arr [ i ] ) ; mp . set ( arr [ i ] , ++ x ) ; } } for ( let i = 0 ; i < n ; ++ i ) { if ( mp . get ( arr [ i ] ) >= k ) document . write ( arr [ i ] + " " ) ; } } let arr = [ 1 , 2 , 2 , 3 , 2 , 3 , 4 ] ; let n = arr . length ; let k = 2 ; removeElements ( arr , n , k ) ;
Check if a string contains a palindromic sub | Function to check if two consecutive same characters are present ; Driver code
function check ( s ) { for ( let i = 0 ; i < s . length - 1 ; i ++ ) if ( s [ i ] == s [ i + 1 ] ) return true ; return false ; } let s = " " ; if ( check ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Remove elements from the array which appear more than k times | JavaScript program to remove the elements which appear more than k times from the array . ; Hash map which will store the frequency of the elements of the array . ; Incrementing the frequency of the element by 1. ; Print the element which appear less than or equal to k times . ; Driver code
function RemoveElements ( arr , n , k ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; } for ( let i = 0 ; i < n ; ++ i ) { if ( mp . has ( arr [ i ] ) && mp . get ( arr [ i ] ) <= k ) { document . write ( arr [ i ] + " " ) ; } } } let arr = [ 1 , 2 , 2 , 3 , 2 , 3 , 4 ] ; let n = arr . length ; let k = 2 ; RemoveElements ( arr , n , k ) ;
Find the smallest after deleting given elements | Returns minimum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the smallestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it smallestElement ; Driver Code
function findSmallestAfterDel ( arr , m , del , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( mp . has ( del [ i ] ) ) { mp . set ( del [ i ] , mp . get ( del [ i ] ) + 1 ) ; } else { mp . set ( del [ i ] , 1 ) ; } } let smallestElement = Number . MAX_VALUE ; for ( let i = 0 ; i < m ; ++ i ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) - 1 ) ; if ( mp . get ( arr [ i ] ) == 0 ) mp . delete ( arr [ i ] ) ; } else smallestElement = Math . min ( smallestElement , arr [ i ] ) ; } return smallestElement ; } let array = [ 5 , 12 , 33 , 4 , 56 , 12 , 20 ] ; let m = array . length ; let del = [ 12 , 4 , 56 , 5 ] ; let n = del . length ; document . write ( findSmallestAfterDel ( array , m , del , n ) ) ;