Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find the element that appears once in an array where every oth… #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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