Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// C++ program to find
// element that appears once
#include <bits/stdc++.h>

using namespace std;

// function which find number
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];
}

// applying the formula.
return 2 * (sum1) - sum2;
}

// Driver code
int main()
{
int a[] = {2, 3, 5, 4, 5, 3, 4};
int n = 7;
cout << singleNumber(a,n) << "\n";

int b[] = {15, 18, 16, 18, 16, 15, 89};

cout << singleNumber(b,n);
return 0;
}

// This code is contributed by mohit kumar 29