原文:

给定一个数组 arr ,任务是按照降序打印数组的元素及其频率。 例:

输入: arr[] = {1,3,3,3,4,4,5} 输出: 5 发生 1 次 4 发生 2 次 3 发生 3 次 1 发生 1 次 输入: arr[] = {1,1,1,2,3,4,9,10} 输出: 10 发生 1 次 9 发生 2 次【t15

简单方法:使用一些(例如),以降序存储元素,然后用它的计数逐个打印元素,然后从数据结构中删除它。对于所使用的数据结构,时间复杂度为 0(n 对数 n),辅助空间为 0(n)。 以下是上述方法的实现:

卡片打印处理机(card print processor 的缩写)

// c   program to print the elements in
// descending along with their frequencies
#include 
using namespace std;
// function to print the elements in descending
// along with their frequencies
void printelements(int a[], int n)
{
    // a multiset to store elements in decreasing order
    multiset > ms;
    // insert elements in the multiset
    for (int i = 0; i < n; i  ) {
        ms.insert(a[i]);
    }
    // print the elements along with their frequencies
    while (!ms.empty()) {
        // find the maximum element
        int maxel = *ms.begin();
        // number of times it occurs
        int times = ms.count(maxel);
        cout << maxel << " occurs " << times << " times\n";
        // erase the maxel
        ms.erase(maxel);
    }
}
// driver code
int main()
{
    int a[] = { 1, 1, 1, 2, 3, 4, 9, 9, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    printelements(a, n);
    return 0;
}

output: 

10 occurs 1 times
9 occurs 2 times
4 occurs 1 times
3 occurs 1 times
2 occurs 1 times
1 occurs 3 times

高效方法: ,然后开始打印元素及其频率。 以下是上述方法的实施:

c

// c   program to print the elements in
// descending along with their frequencies
#include 
using namespace std;
// function to print the elements in descending
// along with their frequencies
void printelements(int a[], int n)
{
    // sorts the element in decreasing order
    sort(a, a   n, greater());
    int cnt = 1;
    // traverse the array elements
    for (int i = 0; i < n - 1; i  ) {
        // prints the number and count
        if (a[i] != a[i   1]) {
            cout << a[i] << " occurs " << cnt << " times\n";
            cnt = 1;
        }
        else
            cnt  = 1;
    }
    // prints the last step
    cout << a[n - 1] << " occurs " << cnt << " times\n";
}
// driver code
int main()
{
    int a[] = { 1, 1, 1, 2, 3, 4, 9, 9, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    printelements(a, n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print the elements in
// descending along with their frequencies
import java.util.*;
class gfg
{
// function to print the elements in descending
// along with their frequencies
static void printelements(int a[], int n)
{
    // sorts the element in decreasing order
    arrays.sort(a);
    a = reverse(a);
    int cnt = 1;
    // traverse the array elements
    for (int i = 0; i < n - 1; i  )
    {
        // prints the number and count
        if (a[i] != a[i   1])
        {
            system.out.print(a[i]  " occurs "  
                            cnt   " times\n");
            cnt = 1;
        }
        else
            cnt  = 1;
    }
    // prints the last step
    system.out.print(a[n - 1]  " occurs "  
                    cnt   " times\n");
}
static int[] reverse(int a[])
{
    int i, n = a.length, t;
    for (i = 0; i < n / 2; i  )
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
}
// driver code
public static void main(string[] args)
{
    int a[] = { 1, 1, 1, 2, 3, 4, 9, 9, 10 };
    int n = a.length;
    printelements(a, n);
}
}
// this code is contributed by princiraj1992

python 3

# python3 program to print the elements in
# descending along with their frequencies
# function to print the elements in
# descending along with their frequencies
def printelements(a, n) :
    # sorts the element in decreasing order
    a.sort(reverse = true)
    cnt = 1
    # traverse the array elements
    for i in range(n - 1) :
        # prints the number and count
        if (a[i] != a[i   1]) :
            print(a[i], " occurs ", cnt, "times")
            cnt = 1
        else :
            cnt  = 1
    # prints the last step
    print(a[n - 1], "occurs", cnt, "times")
# driver code
if __name__ == "__main__" :
    a = [ 1, 1, 1, 2,
          3, 4, 9, 9, 10 ]
    n = len(a)
    printelements(a, n)
# this code is contributed by ryuga

c

// c# program to print the elements in
// descending along with their frequencies
using system;
class gfg
{
// function to print the elements in descending
// along with their frequencies
static void printelements(int []a, int n)
{
    // sorts the element in decreasing order
    array.sort(a);
    a = reverse(a);
    int cnt = 1;
    // traverse the array elements
    for (int i = 0; i < n - 1; i  )
    {
        // prints the number and count
        if (a[i] != a[i   1])
        {
            console.write(a[i]  " occurs "  
                            cnt   " times\n");
            cnt = 1;
        }
        else
            cnt  = 1;
    }
    // prints the last step
    console.write(a[n - 1]  " occurs "  
                    cnt   " times\n");
}
static int[] reverse(int []a)
{
    int i, n = a.length, t;
    for (i = 0; i < n / 2; i  )
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
}
// driver code
public static void main(string[] args)
{
    int []a = { 1, 1, 1, 2, 3, 4, 9, 9, 10 };
    int n = a.length;
    printelements(a, n);
}
}
// this code is contributed by princiraj1992

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


output: 

10 occurs 1 times
9 occurs 2 times
4 occurs 1 times
3 occurs 1 times
2 occurs 1 times
1 occurs 3 times