Daily Dose of DSA - Day 8
Table of contents
class Solution
{
public:
int missingNumber(vector<int> &nums) //
{
int ans = 0;
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); i++)
m[nums[i]]++;
for (int i = 0; i <= nums.size(); i++)
{
if (!(m.find(i) != m.end()))
// if(m.count(i)==0)
{
ans = i;
}
}
return ans;
}
};
//In the XOR method we take bitwise XOR of indexes and the number, if indexes and number are present in the process their effect gets cancelled and the extra element is remained .
class Solution
{
public:
int missingNumber(vector<int> &nums)
{
int n = nums.size();
int x = 0;
for (int i = 0; i < n; i++) //
{
x ^= i;
x ^= nums[i];
}
x ^= n;
return x;
}
};
class Solution
{
public:
int missingNumber(vector<int> &nums)
{
int n = nums.size();
int x = (n * (n + 1)) / 2;
int y = 0;
for (int i = 0; i < n; i++)
{
y = y + nums[i];
}
return x - y;
}
};
Subscribe to my newsletter
Read articles from Atharva directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Atharva
Atharva
Hi, I'm Atharva Martiwar, a pre-final year student at National Institute of Technology Rourkela doing my bachelor's degree in Electronics and Electrical Engineering. As a Computer Science & CP Enthusiast, I am keen on problem-solving and logical reasoning through coding. Love to learn DSA and solve coding questions. Good proficiency in C++, Dart, and Python. Currently exploring flutter for Android app development and python for Automation, building web, and Desktop Applications. Apart from my technical skills, I have a great experience in event management and planning. Through experiences and voluntary efforts, I have managed to develop leadership qualities and proficiency in speaking skills. PS: Love to play Badminton & Table Tennis :D