Current streak:
0 days
Longest streak:
25 days
Less
More
#include <bits/stdc++.h> vector<pair<char,int>> duplicate_char(string s, int n){ map<char,int> mp; for(auto i:s) mp[i]++; vector<pair<char,int>> ans; for(auto i:mp) { if(i.second>=2) { pair<char,int> p; p.first=i.first; p.second=i.second; ans.push_back(p); } } return ans; }
bool comp(vector<int>& v1, vector<int>& v2) { if(v1[0] != v2[0]) { return v1[0] > v2[0]; } else { return v1[1] < v2[1]; } }
vector<int> sortByFrequency(vector<int>& nums) { unordered_map<int, int> d; unordered_map<int, int> index; // For each array element, insert into the dictionary its frequency and index of its first occurrence in the array for(int i = 0; i < nums.size(); i++) { d[nums[i]] += 1; if(index.find(nums[i]) == index.end()) { index[nums[i]] = i; } }
vector<vector<int>> lst; for(auto it: d) { vector<int> temp; temp.push_back(it.second); temp.push_back(index[it.first]); temp.push_back(it.first); lst.push_back(temp); } // Sort the values based on a custom comparator. sort(lst.begin(), lst.end(), comp); // Finally return the sorted list having arranged items. vector<int> result; for(int i = 0; i < lst.size(); i++) { for(int j = 1; j <= lst[i][0]; j++) { result.push_back(lst[i][2]); } }
return result; }