Find the element that appears once in an array where every other element appears twice | C ++ program to find element that appears once ; function which find number ; applying the formula . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int singleNumber ( int nums [ ] , int n ) { map < int , int > m ; long sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m [ nums [ i ] ] == 0 ) { sum1 += nums [ i ] ; m [ nums [ i ] ] ++ ; } sum2 += nums [ i ] ; } return 2 * ( sum1 ) - sum2 ; } int main ( ) { int a [ ] = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int n = 7 ; cout << singleNumber ( a , n ) << " STRNEWLINE " ; int b [ ] = { 15 , 18 , 16 , 18 , 16 , 15 , 89 } ; cout << singleNumber ( b , n ) ; return 0 ; }