hi, let Avik connect you.
** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.
** You can check all the posts on the Posts page.
** More about me in the About Me section.
Distinct Enemies
Problem: Distinct Enemies
Problem Link: www.codingninjas.com/codestudio/problems/distinct-enemies_1474981
Approach:
Initiate a hashmap.
Traverse through all the given elements.
Once an element is encountered mark it as appeared.
Return the size of the hashmap.
Time and Space Complexity:
Time Complexity: O(N)
Space Complexity: O(N)
Code [C++]
#include <bits/stdc++.h>
using namespace std;
int distinctEnemies(vector<int> &arr, int n)
{
unordered_map<int, bool>mymap;
for(int i=0; i<n; i++) mymap[arr[i]] = true;
return mymap.size();
}