LCM of unique elements present in an array | C # program for the above approach ; Function to find GCD of two numbers ; Base Case ; Recursively find the GCD ; Function to find LCM of two numbers ; Function to find LCM of unique elements present in the array ; Stores the frequency of each number of the array ; Store the frequency of each element of the array ; Store the required result ; Traverse the map freq ; If the frequency of the current element is 1 , then update ans ; If there is no unique element , set lcm to - 1 ; Print the result ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int findGCD ( int a , int b ) { if ( b == 0 ) return a ; return findGCD ( b , a % b ) ; } static int findLCM ( int a , int b ) { return ( a * b ) / findGCD ( a , b ) ; } static void uniqueElementsLCM ( int [ ] arr , int N ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) ) freq [ arr [ i ] ] ++ ; else freq . Add ( arr [ i ] , 1 ) ; } int lcm = 1 ; foreach ( KeyValuePair < int , int > kvp in freq ) { if ( kvp . Value == 1 ) { lcm = findLCM ( lcm , kvp . Key ) ; } } if ( lcm == 1 ) lcm = - 1 ; Console . Write ( lcm ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 1 , 3 , 3 , 4 } ; int N = arr . Length ; uniqueElementsLCM ( arr , N ) ; } }