Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
sortbyfrequency(hasing+sorting using comparator function)
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#include<bits/stdc++.h> using namespace std; bool compare(pair<int,int>p1,pair<int,int>p2) { if(p1.second!=p2.second) { return (p1.second>p2.second); } else { return (p1.first>p2.second); } } void sortByFreq(int arr[],int n) { //Your code here unordered_map<int,int>m; for(int i=0;i<n;i++) { m[arr[i]]++; } vector<pair<int,int>>v; for(auto it=m.begin();it!=m.end();it++) { v.push_back(make_pair(it->first,it->second)); } sort(v.begin(),v.end(),compare); for(int i=0;i<v.size();i++) { int k=v[i].second; while(k--) { cout<<v[i].first<<" "; } } cout<<"\n"; } int main() { int t; cin>>t; while(t--) { int n; cin>>n; int arr[n]; for(int i=0;i<n;i++) { cin>>arr[i]; } sortByFreq(arr,n); } }
g++
2 5 5 5 4 6 4 5 9 9 9 2 5
Show compiler warnings
[
+
] Compiler args
[
-
]
Show input
Compilation time: 2.4 sec, absolute running time: 0.17 sec, cpu time: 0.01 sec, memory peak: 3 Mb, absolute service time: 2,66 sec
edit mode
|
history
|
discussion
4 4 5 5 6 9 9 9 2 5