http://www.practice.geeksforgeeks.org/problem-page.php?pid=493

Sorting Elements of an Array by Frequency

Given an array of integers, sort the array according to frequency of elements. For example, if the input array is {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12}, then modify the array to {3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}.

If frequencies of two elements are same, print them in increasing order.

Input:

The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.

Output:

Print each sorted array in a seperate line. For each array its numbers should be seperated by space.

Constraints:

1 ≤ T ≤ 70
30 ≤ N ≤ 130
1 ≤ A [ i ] ≤ 60

Example:

Input:

1
5
5 5 4 6 4

Output:

4 4 5 5 6

import java.util.*;
import java.lang.*;
import java.io.*; class pair {
public int freq;
public int key;
public pair(int f, int k) {
super();
this.freq = f;
this.key = k;
}
} class cmp implements Comparator<pair> { public int compare(pair p1, pair p2) {
if(p1.freq != p2.freq) {
return p2.freq - p1.freq;
} else {
return p1.key - p2.key;
}
}
} class GFG { public static void pln(Object[] ls) {
for(int i=0; i<ls.length; ++i) {
System.out.print(ls[i]);
} System.out.println();
} public static void func(int[] arr) { int n = arr.length;
HashMap<Integer, Integer> mapping = new HashMap<Integer, Integer> (); for(int na: arr) {
if(!mapping.containsKey(na)) {
mapping.put(na, 0);
}
mapping.put(na, mapping.get(na) + 1);
} TreeSet<pair> freqToKey = new TreeSet<pair> (new cmp());
Iterator iter = mapping.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
int key = (int) entry.getKey();
int freq = (int) entry.getValue();
pair p = new pair(freq, key);
freqToKey.add(p);
} Object[] ls = freqToKey.toArray();
for(int i=0; i<ls.length; ++i) {
pair p = (pair) ls[i];
int freq = p.freq;
int key = p.key; while(freq > 0) {
--freq;
System.out.print(key + " ");
}
} System.out.println();
} public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int times = in.nextInt(); while(times > 0) {
--times; int n = in.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; ++i) {
arr[i] = in.nextInt();
} func(arr);
}
}
}

geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)的更多相关文章

  1. Array类的Sort()方法

    刚复习了Array类的sort()方法, 这里列举几个常用的,和大家一起分享. Array类实现了数组中元素的冒泡排序.Sort()方法要求数组中的元素实现IComparable接口.如System. ...

  2. 【leetcode】1144. Decrease Elements To Make Array Zigzag

    题目如下: Given an array nums of integers, a move consists of choosing any element and decreasing it by ...

  3. 【LeetCode】1464. 数组中两元素的最大乘积 Maximum Product of Two Elements in an Array (Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找最大次大 日期 题目地址:https://le ...

  4. 排序算法 (sorting algorithm)之 冒泡排序(bubble sort)

    http://www.algolist.net/Algorithms/ https://docs.oracle.com/javase/tutorial/collections/algorithms/ ...

  5. [Algorithms] Divide and Recurse Over an Array with Merge Sort in JavaScript

    Merge sort is a recursive sorting algorithm. If you don't understand recursion, I recommend finding ...

  6. .NET中string[]数组和List<string>泛型的相互转换以及Array类的Sort()方法(转)

    从string[]转List<string>: " }; List<string> list = new List<string>(str); 从List ...

  7. js中的数组Array定义与sort方法使用示例

    Array的定义及sort方法使用示例 Array数组相当于java中的ArrayList  定义方法:  1:使用new Array(5  )创建数组 var ary = new Array(5): ...

  8. 排序算法(sorting algorithm) 之 选择排序(selection sort)

    https://en.wikipedia.org/wiki/Selection_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 4,6,1,3,7 -> ,3,7 1 ...

  9. 排序算法(sorting algorithm)之 插入排序(insertion sort)

    https://en.wikipedia.org/wiki/Insertion_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 loop2: 4,6,1,3,7 -> ...

随机推荐

  1. Spring 注解@Transactional

    Spring事务的传播行为 在service类前加上@Transactional,声明这个service所有方法需要事务管理.每一个业务方法开始时都会打开一个事务. Spring默认情况下会对运行期例 ...

  2. linux学习之centos(二):虚拟网络三种连接方式和SecureCRT的使用

    ---操作环境--- 虚拟机版本:VMware Workstation_10.0.3 Linux系统版本:CentOS_6.5(64位) 物理机系统版本:win10  一.虚拟网络三种连接方式 当在V ...

  3. getHitRect获取点击控件的位置

    public  void getHitRect(Rect outRect)                   Added in API level 1 Hit rectangle in parent ...

  4. 事务&视图和索引

    一:事务 1.含义:事务是一个不可分割的整体,事务中的多个执行过程,同生共死.要么都执行成功,要么都执行失败. 事务必须具备以下四个属性,(简称:ACID): ①.原子性(Atomicity):事务的 ...

  5. POJ 3254 (状压DP) Corn Fields

    基础的状压DP,因为是将状态压缩到一个整数中,所以会涉及到很多比较巧妙的位运算. 我们可以先把输入中每行的01压缩成一个整数. 判断一个状态是否有相邻1: 如果 x & (x << ...

  6. HDU 4023 (博弈 贪心 模拟) Game

    如果硬要说这算是博弈题目的话,那这个博弈是不公平博弈(partizan games),因为双方面对同一个局面做出来的决策是不一样的. 我们平时做的博弈都是公平博弈(impartial games),所 ...

  7. Windows bat with adb

    /********************************************************************* * Windows bat with adb * 说明: ...

  8. 【C#学习笔记】数组使用

    using System; namespace ConsoleApplication { class Program { static void Main(string[] args) { //int ...

  9. 一种Javascript解释ajax返回的json的好方法

    通常ajax请求返回的格式为json或者xml,如果返回的是json,则可以通过转换成javascript对象进行操作,如下: 1.ajax请求的controller实现 @RequestMappin ...

  10. Arduino 电平转换 升压 OUTPUT与9V/12V元件通信

    原因 网络上有不少怎么让Arduino的5V电平转换成3.3V电平,从而和工作在3.3V下的芯片相互沟通的教程.但是如果想驱动高于5V电压的芯片,就找不到教程了.因此今天我来介绍一种方式,能让Ardu ...