Find index of an extra element present in one sorted array | Java code for above approach ; Function to find Index ; Calculating extra element ; returns index of extra element ; function return sum of array elements ; function return index of given element ; Driver Code
class GFG { static int find_extra_element_index ( int [ ] arrA , int [ ] arrB ) { int extra_element = sum ( arrA ) - sum ( arrB ) ; return indexOf ( arrA , extra_element ) ; } static int sum ( int [ ] arr ) { int sum = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { sum += arr [ i ] ; } return sum ; } static int indexOf ( int [ ] arr , int element ) { for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == element ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arrA = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int [ ] arrB = { 2 , 4 , 6 , 8 , 10 , 12 } ; System . out . println ( find_extra_element_index ( arrA , arrB ) ) ; } }