Using map to solve the problem


  • 0
    Z

    Using the map template to solve the problem.

    int singleNumber(int A[], int n) {
    map<int, int> countMap;
    int num = 0;
    if (n <= 0)
    	return -1;
    for (int i = 0; i< n; i++)
    {
    	if (countMap.find(A[i]) != countMap.end())
    		countMap[A[i]] += 1;
    	else
    		countMap[A[i]] = 1;;
    }
    
    for (int i = 0; i < n; i++)
    {
    	if (countMap[A[i]] == 1)
    		return A[i];
    }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.