[email protected] Sorting Elements of an Array by Frequency (Sort)

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);
        }
    }
}

时间: 2024-07-31 01:27:07

[email protected] Sorting Elements of an Array by Frequency (Sort)的相关文章

[email&#160;protected] Largest Number formed from an Array

http://www.practice.geeksforgeeks.org/problem-page.php?pid=380 Largest Number formed from an Array Given a list of non negative integers, arrange them in such a manner that they form the largest number possible. The result is going to be very large,

[email&#160;protected]:php

curl 获取页面信息 function curl_get_content($url){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) App

Shell特殊变量:Shell $0, $#, $*, [email&#160;protected], $?, $$和命令行参数

变量名只能包含数字.字母和下划线,因为某些包含其他字符的变量有特殊含义,这样的变量被称为特殊变量. 例如,$ 表示当前Shell进程的ID,即pid,看下面的代码: $echo $$ 运行结果 29949 特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个参数是$1,第二个参数是$2. $# 传递给脚本或函数的参数个数. $* 传递给脚本或函数的所有参数. [email protected] 传递给脚本或函数的所有参数.被

OC基础[email&#160;protected]后的修饰符及其用法小结

[email protected]后的修饰符 strong : 该属性值对应 __strong 关键字,即该属性所声明的变量将成为对象的持有者,等同于"retain" weak : 该属性对应 __weak 关键字,与 __weak 定义的变量一致,该属性所声明的变量将没有对象的所有权,并且当对象被释放之后,对象将被自动赋值nil,记住IBOutlet应该使用weakcopy : 和之前的copy一样,复制一个对象并创建strong关联assign : 对象不能使用assign,但原始

@codeforces - [email&#160;protected] Take Metro

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 环上有 n 个点,按顺时针顺序以 1 到 n 编号.其中 1~m 号点是红色的,m+1~n 号点时蓝色的. 一开始你位于点 s,并给定一个 t. 你需要重复以下步骤,直到 t = 0: 如果你所在结点为红色,顺时针移动 t 个点:否则逆时针移动 t 个点.然后 t 减一. 求最终所在的

$*和[email&#160;protected]之间区别代码分析

#!/bin/bash set 'apple pie' pears peaches for i in $*           /*单引号被去掉,循环单个字符输出*/ do echo $i done [[email protected] Ex_14.02-14.31]# sh 14-14-1 apple pie pears peaches -------------------------------------------------------------- #!/bin/bash set

[email&#160;protected]一个高效的配置管理工具--Ansible configure management--翻译(六)

无书面许可请勿转载 高级playbook Finding files with variables All modules can take variables as part of their arguments by dereferencing them with {{ and }} . You can use this to load a particular file based on a variable. For example, you might want to select a

【转载】 ERROR 1045 (28000): Access denied for user [email&#160;protected] (using password: NO)

来自:http://www.jb51.net/LINUXjishu/10981.html 错误描述: Mysql中添加用户之后可能出现登录时提示ERROR 1045 (28000): Access denied for user的错误.删除user.user中值为NULL的,或更新NULL为test 1)delete from user where user is NULL 2)update user set user='test' where user is NULL.意外的情况: 如果上述方

[[email&#160;protected]] Omit catch error block if not needed

From [email protected], you can omit catch error block. Before: try { throw new Error('whatever'); } catch(err) { console.log(err) } Now: try { throw new Error('whatever'); } catch { console.log("error happened") } It is just a syntax sugar, if