Can anyone explain this code (function) please ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone explain this code (function) please ?

Can anyone please explain this code(function) void Removeduplicate(int arr[],int n) { unordered_map<int,bool> mp; int i; /*// if(mp.empty()) // { // cout<<"How it's working with empty map elemnts !"<<endl; // } */ for(i = 0; i < n; i++) { if(mp.find(arr[i]) == mp.end()) { cout<<arr[i]<<' '; } mp[arr[i]] = true; } cout<<endl; }

25th Nov 2020, 7:09 AM
Dave
1 Answer
+ 4
The function accepts an array and the array length. It creates an unordered_map object and then it iterates the array, checking whether array element at index i-th has been mapped as key for an item in the unordered_map. When an array element isn't mapped as an item in the unordered_map, that element will be printed on screen. An item will be created in the unordered_map using each array element as key, and a boolean true as value.
25th Nov 2020, 8:02 AM
Ipang