codeparrot/xlcost-text-to-code huggingface dataset:
Group words with same set of characters | C # program to print all words that have the same unique character set ; Generates a key from given string . The key contains all unique characters of given string in sorted order consisting of only distinct elements . ; Store all unique characters of current word in key ; Print all words together with same character sets . ; Stores indexes of all words that have same set of unique characters . unordered_map < string , vector < int > > Hash ; ; Traverse all words ; If the key is already in the map then get its corresponding value and update the list and put it in the map ; If key is not present in the map then create a new list and add both key and the list ; Print all words that have the same unique character set ; Driver code
using System ; using System . Collections . Generic ; class GFG { static readonly int MAX_CHAR = 26 ; static String getKey ( String str ) { bool [ ] visited = new bool [ MAX_CHAR ] ; for ( int j = 0 ; j < str . Length ; j ++ ) visited [ str [ j ] - ' a ' ] = true ; String key = " " ; for ( int j = 0 ; j < MAX_CHAR ; j ++ ) if ( visited [ j ] ) key = key + ( char ) ( ' a ' + j ) ; return key ; } static void wordsWithSameCharSet ( String [ ] words , int n ) { Dictionary < String , List < int > > Hash = new Dictionary < String , List < int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { String key = getKey ( words [ i ] ) ; if ( Hash . ContainsKey ( key ) ) { List < int > get_al = Hash [ key ] ; get_al . Add ( i ) ; Hash [ key ] = get_al ; } else { List < int > new_al = new List < int > ( ) ; new_al . Add ( i ) ; Hash . Add ( key , new_al ) ; } } foreach ( KeyValuePair < String , List < int > > it in Hash ) { List < int > get = it . Value ; foreach ( int v in get ) Console . Write ( words [ v ] + " , ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { String [ ] words = { " may " , " student " , " students " , " dog " , " studentssess " , " god " , " cat " , " act " , " tab " , " bat " , " flow " , " wolf " , " lambs " , " amy " , " yam " , " balms " , " looped " , " poodle " } ; int n = words . Length ; wordsWithSameCharSet ( words , n ) ; } }
Second most repeated word in a sequence | C # program to find out the second most repeated word ; Method to find the word ; Store all the words with its occurrence ; find the second largest occurrence ; Return string with occurrence equals to sec_max ; Driver method
using System ; using System . Collections . Generic ; class GFG { static String secMostRepeated ( List < String > seq ) { Dictionary < String , int > occ = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < seq . Count ; i ++ ) if ( occ . ContainsKey ( seq [ i ] ) ) occ [ seq [ i ] ] = occ [ seq [ i ] ] + 1 ; else occ . Add ( seq [ i ] , 1 ) ; int first_max = int . MinValue , sec_max = int . MinValue ; foreach ( KeyValuePair < String , int > entry in occ ) { int v = entry . Value ; if ( v > first_max ) { sec_max = first_max ; first_max = v ; } else if ( v > sec_max && v != first_max ) sec_max = v ; } foreach ( KeyValuePair < String , int > entry in occ ) { int v = entry . Value ; if ( v == sec_max ) return entry . Key ; } return null ; } public static void Main ( String [ ] args ) { String [ ] arr = { " ccc " , " aaa " , " ccc " , " ddd " , " aaa " , " aaa " } ; List < String > seq = new List < String > ( arr ) ; Console . WriteLine ( secMostRepeated ( seq ) ) ; } }
Check if a Binary Tree ( not BST ) has duplicate values | C # Program to check duplicates in Binary Tree ; Function that used HashSet to find presence of duplicate nodes ; If tree is empty , there are no duplicates . ; If current node 's data is already present.  ; Insert current node ; Recursively check in left and right subtrees . ; To check if tree has duplicates ; A binary tree Node has data , pointer to left child and a pointer to right child
using System ; using System . Collections ; using System . Collections . Generic ; class CheckDuplicateValues { public static Boolean checkDupUtil ( Node root , HashSet < int > s ) { if ( root == null ) return false ; if ( s . Contains ( root . data ) ) return true ; s . Add ( root . data ) ; return checkDupUtil ( root . left , s ) || checkDupUtil ( root . right , s ) ; } public static Boolean checkDup ( Node root ) { HashSet < int > s = new HashSet < int > ( ) ; return checkDupUtil ( root , s ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 2 ) ; root . left . left = new Node ( 3 ) ; if ( checkDup ( root ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } } public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; } } ;
Expression Tree | C # program to construct an expression tree ; An expression tree node ; A utility function to check if ' c ' is an operator ; Utility function to do inorder traversal ; Returns root of constructed tree for given postfix expression ; Traverse through every character of input expression ; If operand , simply Push into stack ; operator ; Pop two top nodes Store top Remove top ; make them children ; System . out . println ( t1 + " " + t2 ) ; Add this subexpression to stack ; only element will be root of expression tree ; Driver code
using System ; using System . Collections ; public class Node { public char value ; public Node left , right ; public Node ( char item ) { value = item ; left = right = null ; } } class ExpressionTree { Boolean isOperator ( char c ) { if ( c == ' + ' c == ' - ' c == ' * ' c == ' / ' c == ' ^ ' ) { return true ; } return false ; } void inorder ( Node t ) { if ( t != null ) { inorder ( t . left ) ; Console . Write ( t . value + " ▁ " ) ; inorder ( t . right ) ; } } Node constructTree ( char [ ] postfix ) { Stack st = new Stack ( ) ; Node t , t1 , t2 ; for ( int i = 0 ; i < postfix . Length ; i ++ ) { if ( ! isOperator ( postfix [ i ] ) ) { t = new Node ( postfix [ i ] ) ; st . Push ( t ) ; } else { t = new Node ( postfix [ i ] ) ; t1 = ( Node ) st . Pop ( ) ; t2 = ( Node ) st . Pop ( ) ; t . right = t1 ; t . left = t2 ; st . Push ( t ) ; } } t = ( Node ) st . Peek ( ) ; st . Pop ( ) ; return t ; } public static void Main ( String [ ] args ) { ExpressionTree et = new ExpressionTree ( ) ; String postfix = " ab + ef * g * - " ; char [ ] charArray = postfix . ToCharArray ( ) ; Node root = et . constructTree ( charArray ) ; Console . WriteLine ( " infix ▁ expression ▁ is " ) ; et . inorder ( root ) ; } }
Queries to count sum of rows and columns of a Matrix present in given ranges |  ; Function to preprocess the matrix to execute the queries ; Stores the sum of each row ; Stores the sum of each col ; Traverse the matrix and calculate sum of each row and column ; Insert all row sums in sum_list ; Insert all column sums in sum_list ; Sort the array in ascending order ; Traverse the array queries [ ] [ ] ; Search the leftmost index of L ; Search the rightmost index of R ; Function to search for the leftmost index of given number ; Initialize low , high and ans ; Stores mid ; If A [ mid ] >= num ; Function to search for the rightmost index of given number ; Initialise low , high and ans ; Stores mid ; If A [ mid ] <= num ; Update ans ; Update mid ; Update high ; driver code ; Given dimensions of matrix ; Given matrix ; Given number of queries ; Given queries ; Function call to count the number row - sums and column - sums present in the given ranges
using System ; using System . Collections . Generic ; class GFG { static void totalCount ( int [ , ] A , int N , int M , int [ , ] queries , int Q ) { int [ ] row_sum = new int [ N ] ; int [ ] col_sum = new int [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { row_sum [ i ] += A [ i , j ] ; col_sum [ j ] += A [ i , j ] ; } } List < int > sum_list = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) sum_list . Add ( row_sum [ i ] ) ; for ( int i = 0 ; i < M ; i ++ ) sum_list . Add ( col_sum [ i ] ) ; sum_list . Sort ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { int L = queries [ i , 0 ] ; int R = queries [ i , 1 ] ; int l = left_search ( sum_list , L ) ; int r = right_search ( sum_list , R ) ; Console . Write ( r - l + 1 + " ▁ " ) ; } } static int left_search ( List < int > A , int num ) { int low = 0 , high = A . Count - 1 ; int ans = 0 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( A [ mid ] >= num ) { ans = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } return ans ; } static int right_search ( List < int > A , int num ) { int low = 0 , high = A . Count - 1 ; int ans = high ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( A [ mid ] <= num ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } static void Main ( ) { int N = 3 , M = 2 ; int [ , ] A = new int [ , ] { { 13 , 3 } , { 9 , 4 } , { 6 , 10 } } ; int Q = 2 ; int [ , ] queries = new int [ , ] { { 10 , 20 } , { 25 , 35 } } ; totalCount ( A , N , M , queries , Q ) ; } }
Smallest element repeated exactly ‘ k ’ times ( not limited to small range ) | C # program to find the smallest element with frequency exactly k . ; Map is used to store the count of elements present in the array ; Traverse the map and find minimum element with frequency k . ; Driver code
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { public static int smallestKFreq ( int [ ] a , int n , int k ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( m . ContainsKey ( a [ i ] ) ) { var v = m [ a [ i ] ] ; m . Remove ( a [ i ] ) ; m . Add ( a [ i ] , v + 1 ) ; } else m . Add ( a [ i ] , 1 ) ; int res = int . MaxValue ; HashSet < int > s = new HashSet < int > ( m . Keys . ToArray ( ) ) ; foreach ( int temp in s ) if ( m [ temp ] == k ) res = Math . Min ( res , temp ) ; return ( res != int . MaxValue ) ? res : - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , 1 , 3 , 1 } ; int k = 2 ; Console . WriteLine ( smallestKFreq ( arr , arr . Length , k ) ) ; } }
Maximize profit that can be earned by selling an item among N buyers | C # Program to implement the above approach ; Function to find the maximum profit earned by selling an item among N buyers ; Stores the maximum profit ; Stores the price of the item ; Sort the array ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Update the maximum profit ; Return the maximum possible price ; Driver Code
using System ; class GFG { public static int maximumProfit ( int [ ] arr ) { int ans = Int32 . MinValue ; int price = 0 ; Array . Sort ( arr ) ; int N = arr . Length ; for ( int i = 0 ; i < N ; i ++ ) { int count = ( N - i ) ; if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } public static void Main ( String [ ] args ) { int [ ] arr = { 22 , 87 , 9 , 50 , 56 , 43 } ; Console . WriteLine ( maximumProfit ( arr ) ) ; } }
Lexicographically smallest permutation of the array possible by at most one swap | C # program for the above approach ; Function to print the elements of the array arr [ ] ; Traverse the array arr [ ] ; Function to convert given array to lexicographically smallest permutation possible by swapping at most one pair ; Stores the index of the first element which is not at its correct position ; Checks if any such array element exists or not ; Traverse the given array ; If element is found at i ; If the first array is not in correct position ; Store the index of the first elements ; Store the index of the first element ; Swap the pairs ; Print the array ; Driver Code ; Given array ; Store the size of the array
using System ; class GFG { static void print ( int [ ] arr ) { foreach ( int element in arr ) { Console . Write ( element + " ▁ " ) ; } } static void makeLexicographically ( int [ ] arr , int length ) { int index = 0 ; int temp = 0 ; int check = 0 ; int condition = 0 ; int element = 0 ; for ( int i = 0 ; i < length ; ++ i ) { if ( element == arr [ i ] ) { check = i ; break ; } else if ( arr [ i ] != i + 1 && check == 0 ) { index = i ; check = 1 ; condition = - 1 ; element = i + 1 ; } } if ( condition == - 1 ) { temp = arr [ index ] ; arr [ index ] = arr [ check ] ; arr [ check ] = temp ; } print ( arr ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; makeLexicographically ( arr , N ) ; } }
Check if uppercase characters in a string are used correctly or not |  ; Function to check if the character c is in lowercase or not ; Function to check if the character c is in uppercase or not ; Utility function to check if uppercase characters are used correctly or not ; Length of string ; If the first character is in lowercase ; Otherwise ; Check if all characters are in uppercase or not ; If all characters are in uppercase ; Check if all characters except the first are in lowercase ; Function to check if uppercase characters are used correctly or not ; Stores whether the use of uppercase characters are correct or not ; If correct ; Otherwise ; Driver Code ; Given string ; Function call to check if use of uppercase characters is correct or not
using System ; public class GFG { static bool isLower ( char c ) { return c >= ' a ' && c <= ' z ' ; } static bool isUpper ( char c ) { return c >= ' A ' && c <= ' Z ' ; } static bool detectUppercaseUseUtil ( string S ) { int N = S . Length ; int i ; if ( isLower ( S [ 0 ] ) ) { i = 1 ; while ( i < N && isLower ( S [ i ] ) ) ++ i ; return i == N ? true : false ; } else { i = 1 ; while ( i < N && isUpper ( S [ i ] ) ) ++ i ; if ( i == N ) return true ; else if ( i > 1 ) return false ; while ( i < N && isLower ( S [ i ] ) ) ++ i ; return i == N ? true : false ; } } static void detectUppercaseUse ( string S ) { bool check = detectUppercaseUseUtil ( S ) ; if ( check ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } static public void Main ( ) { string S = " GeeKs " ; detectUppercaseUse ( S ) ; } }
Queries to count array elements from a given range having a single set bit | C # implementation for above approach ; Check if N has only one set bit ; Function to build segment tree ; If se is leaf node ; Update the node ; Stores mid value of segment [ ss , se ] ; Recursive call for left Subtree ; Recursive call for right Subtree ; Update the Current Node ; Function to update a value at Xth index ; If ss is equal to X ; Update the Xth node ; Update the tree ; Stores the mid of segment [ ss , se ] ; Update current node ; Count of numbers having one set bit ; If L > se or R < ss Invalid Range ; If [ ss , se ] lies inside the [ L , R ] ; Stores the mid of segment [ ss , se ] ; Return the sum after recursively traversing left and right subtree ; Function to solve queries ; Initialise Segment tree ; Build segment tree ; Perform queries ; Driver code ; Input ; Function call to solve queries
using System ; using System . Collections . Generic ; class GFG { static int check ( int N ) { if ( N == 0 || ( N & ( N - 1 ) ) != 0 ) return 0 ; return 1 ; } static void build_seg_tree ( int ss , int se , int si , int [ ] tree , int [ ] arr ) { if ( ss == se ) { tree [ si ] = check ( arr [ ss ] ) ; return ; } int mid = ( ss + se ) / 2 ; build_seg_tree ( ss , mid , 2 * si + 1 , tree , arr ) ; build_seg_tree ( mid + 1 , se , 2 * si + 2 , tree , arr ) ; tree [ si ] = tree [ 2 * si + 1 ] + tree [ 2 * si + 2 ] ; } static void update ( int ss , int se , int si , int X , int V , int [ ] tree , int [ ] arr ) { if ( ss == se ) { if ( ss == X ) { arr [ X ] = V ; tree [ si ] = check ( V ) ; } return ; } int mid = ( ss + se ) / 2 ; if ( X <= mid ) update ( ss , mid , 2 * si + 1 , X , V , tree , arr ) ; else update ( mid + 1 , se , 2 * si + 2 , X , V , tree , arr ) ; tree [ si ] = tree [ 2 * si + 1 ] + tree [ 2 * si + 2 ] ; } static int query ( int l , int r , int ss , int se , int si , int [ ] tree ) { if ( r < ss l > se ) return 0 ; if ( l <= ss && r >= se ) return tree [ si ] ; int mid = ( ss + se ) / 2 ; return query ( l , r , ss , mid , 2 * si + 1 , tree ) + query ( l , r , mid + 1 , se , 2 * si + 2 , tree ) ; } static void Query ( int [ ] arr , int N , List < List < int > > Q ) { int [ ] tree = new int [ 4 * N ] ; build_seg_tree ( 0 , N - 1 , 0 , tree , arr ) ; for ( int i = 0 ; i < ( int ) Q . Count ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) Console . Write ( query ( Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 , N - 1 , 0 , tree ) + " ▁ " ) ; else update ( 0 , N - 1 , 0 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , tree , arr ) ; } } static void Main ( ) { int [ ] arr = { 12 , 11 , 16 , 2 , 32 } ; List < List < int > > Q = new List < List < int > > ( ) ; Q . Add ( new List < int > ( new int [ ] { 1 , 0 , 2 } ) ) ; Q . Add ( new List < int > ( new int [ ] { 2 , 4 , 24 } ) ) ; Q . Add ( new List < int > ( new int [ ] { 1 , 1 , 4 } ) ) ; int N = arr . Length ; Query ( arr , N , Q ) ; } }
Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X | C # program for the above approach ; Function to check if sum of first N natural numbers is >= X ; Finds minimum value of N such that sum of first N natural number >= X ; Binary Search ; Checks if sum of first ' mid ' natural numbers is greater than equal to X ; Update res ; Update high ; Update low ; Driver Code ; Input ; Finds minimum value of N such that sum of first N natural number >= X
using System ; class GFG { static bool isGreaterEqual ( int N , int X ) { return ( N * ( N + 1 ) / 2 ) >= X ; } static int minimumPossible ( int X ) { int low = 1 , high = X , res = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( isGreaterEqual ( mid , X ) ) { res = mid ; high = mid - 1 ; } else low = mid + 1 ; } return res ; } static public void Main ( ) { int X = 14 ; Console . Write ( minimumPossible ( X ) ) ; } }
Find sum of non | C # Find the sum of all non - repeated elements in an array ; Find the sum of all non - repeated elements in an array ; sort all elements of array ; Driver code
using System ; class GFG { static int findSum ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int sum = arr [ 0 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { sum = sum + arr [ i + 1 ] ; } } return sum ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 1 , 1 , 4 , 5 , 6 } ; int n = arr . Length ; Console . WriteLine ( findSum ( arr , n ) ) ; } }
Length of the longest subsequence such that XOR of adjacent elements is equal to K | C # program of the above approach ; Function to find maximum length of subsequence having XOR of adjacent elements equal to K ; Store maximum length of subsequence ; Stores the dp - states ; Base case ; Iterate over the range [ 1 , N - 1 ] ; Iterate over the range [ 0 , i - 1 ] ; If arr [ i ] ^ arr [ j ] = = K ; Update the dp [ i ] ; Update the maximum subsequence length ; If length of longest subsequence is less than 2 then return 0 ; Driver code ; Input ; Print the length of longest subsequence
using System ; class GFG { static int xorSubsequence ( int [ ] a , int n , int k ) { int ans = 0 ; int [ ] dp = new int [ n ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( ( a [ i ] ^ a [ j ] ) == k ) dp [ i ] = Math . Max ( dp [ i ] , dp [ j ] + 1 ) ; } ans = Math . Max ( ans , dp [ i ] ) ; dp [ i ] = Math . Max ( 1 , dp [ i ] ) ; } return ans >= 2 ? ans : 0 ; } static void Main ( ) { int [ ] arr = { 3 , 2 , 4 , 3 , 5 } ; int K = 1 ; int N = arr . Length ; Console . WriteLine ( xorSubsequence ( arr , N , K ) ) ; } }
Check if a Binary Tree contains duplicate subtrees of size 2 or more | C # program to find if there is a duplicate sub - tree of size 2 or more . ; A binary tree Node has data , pointer to left child and a pointer to right child ; This function returns empty string if tree contains a duplicate subtree of size 2 or more . ; If current node is NULL , return marker ; If left subtree has a duplicate subtree . ; Do same for right subtree ; Serialize current subtree ; If current subtree already exists in hash table . [ Note that size of a serialized tree with single node is 3 as it has two marker nodes . ; Function to find if the Binary Tree contains duplicate subtrees of size 2 or more ; Driver code
using System ; using System . Collections . Generic ; class GFG { static char MARKER = ' $ ' ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; } } ; public static String dupSubUtil ( Node root , HashSet < String > subtrees ) { String s = " " ; if ( root == null ) return s + MARKER ; String lStr = dupSubUtil ( root . left , subtrees ) ; if ( lStr . Equals ( s ) ) return s ; String rStr = dupSubUtil ( root . right , subtrees ) ; if ( rStr . Equals ( s ) ) return s ; s = s + root . data + lStr + rStr ; if ( s . Length > 3 && subtrees . Contains ( s ) ) return " " ; subtrees . Add ( s ) ; return s ; } public static String dupSub ( Node root ) { HashSet < String > subtrees = new HashSet < String > ( ) ; return dupSubUtil ( root , subtrees ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( ' A ' ) ; root . left = new Node ( ' B ' ) ; root . right = new Node ( ' C ' ) ; root . left . left = new Node ( ' D ' ) ; root . left . right = new Node ( ' E ' ) ; root . right . right = new Node ( ' B ' ) ; root . right . right . right = new Node ( ' E ' ) ; root . right . right . left = new Node ( ' D ' ) ; String str = dupSub ( root ) ; if ( str . Equals ( " " ) ) Console . Write ( " ▁ Yes ▁ " ) ; else Console . Write ( " ▁ No ▁ " ) ; } }
Count pairs of similar rectangles possible from a given array |  ; Get the count of all pairs of similar rectangles ; Initialize the result value and map to store the ratio to the rectangles ; Calculate the rectangular ratio and save them ; Calculate pairs of similar rectangles from its common ratio ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static int getCount ( int rows , int columns , int [ , ] sides ) { int res = 0 ; Dictionary < double , int > ratio = new Dictionary < double , int > ( ) ; for ( int i = 0 ; i < rows ; i ++ ) { double rectRatio = ( double ) sides [ i , 0 ] / sides [ i , 1 ] ; if ( ! ratio . ContainsKey ( rectRatio ) ) { ratio [ rectRatio ] = 0 ; } ratio [ rectRatio ] = ratio [ rectRatio ] + 1 ; } foreach ( KeyValuePair < double , int > p in ratio ) { int val = p . Value ; if ( val > 1 ) { res += ( val * ( val - 1 ) ) / 2 ; } } return res ; } public static void Main ( string [ ] args ) { int [ , ] A = { { 4 , 8 } , { 10 , 20 } , { 15 , 30 } , { 3 , 6 } } ; int columns = 2 ; int rows = 4 ; Console . WriteLine ( getCount ( rows , columns , A ) ) ; } }
Non | C # program to find first non - repeating element . ; Driver code
using System ; class GFG { static int firstNonRepeating ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( i != j && arr [ i ] == arr [ j ] ) break ; if ( j == n ) return arr [ i ] ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { 9 , 4 , 9 , 6 , 7 , 4 } ; int n = arr . Length ; Console . Write ( firstNonRepeating ( arr , n ) ) ; } }
Non | Efficient C # program to find first non - repeating element . ; Insert all array elements in hash table ; Traverse array again and return first element with count 1. ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int firstNonRepeating ( int [ ] arr , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) ) { var val = m [ arr [ i ] ] ; m . Remove ( arr [ i ] ) ; m . Add ( arr [ i ] , val + 1 ) ; } else { m . Add ( arr [ i ] , 1 ) ; } } for ( int i = 0 ; i < n ; i ++ ) if ( m [ arr [ i ] ] == 1 ) return arr [ i ] ; return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 9 , 4 , 9 , 6 , 7 , 4 } ; int n = arr . Length ; Console . WriteLine ( firstNonRepeating ( arr , n ) ) ; } }