JS 依照元素出現數量排序

同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:

var array = ["1", "2", "4", "5", "2", "4", "2", "3"];
var str = array.toString();
var filter = [];
var result = [];
document.write("原始陣列:" + array + "<br>");

var get = function (str, tar, arr, tmp) {
    if (str.indexOf(tar) >= 0) {
        tmp = str.slice(str.indexOf(tar) + 1);
        arr.push(tar);
        get(tmp, tar, arr, tmp);
    }
    return arr;
}

for (i in array) {
    var elm = [];
    var tmp;
    var fstr = filter.join();
    if (fstr.indexOf(array[i]) >= 0) {
        continue;
    } else {
        var tmp_arr = get(str, array[i], elm, tmp);
        result.push(tmp_arr.length + ':' + tmp_arr[0]);
        filter.push(array[i]);
    }
}

result.sort();
result.reverse();

//按照出現次數排序,不刪除重複元素
var finishArray = [];

for (index in result) {
    var show = result[index].split(':');
    for (i = 0; i < show[0]; i++) {
        finishArray.push(show[1]);
    }
}
document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>");

//按照出現次數排序,刪除重複元素
var finishArray = [];
for (index in result) {
    var show = result[index].split(':');
    finishArray.push(show[1]);
}
document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");

輸出結果:

原始陣列:1,2,4,5,2,4,2,3
按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1
按出現次數排序,刪除重複元素:2,4,5,3,1

You may also like...

1,466,609 Responses

  1. My family always say that I am wasting my time here at web, but I know I
    am getting know-how everyday by reading thes good articles or reviews.

  2. Wallacefex表示:

    sweet bonanza guncel: sweet bonanza guncel – sweet bonanza

  3. Dannyfut表示:

    dark markets tor darknet dark web markets

  4. Wallacefex表示:

    en guvenilir casino siteleri: guvenilir casino siteleri – Casino Siteleri

  5. From there cilia, which are enriched with OR include speak to odorants in the air.

  6. Kristopher表示:

    Dermatology Partners in Joplin, Missouri, “The primary issue with skin tags is usually the places where they create.

  7. Alyssa表示:

    Hi I am so thrilled I found your site, I really found you by accident,
    while I was researching on Google for something else, Regardless I am here now and would just like to say many thanks
    for a remarkable post and a all round thrilling blog (I also love the theme/design), I don’t have time to browse it
    all at the moment but I have bookmarked it and also
    included your RSS feeds, so when I have time I will be back to read more, Please
    do keep up the awesome work.

  8. Video marketing is definitely on the rise! It’s exciting to see how brands are using it creatively. Explore more ideas at Bamsh Digital Marketing

  9. At PPM, our goal is to help establish you and your project up for success from the
    start.

  10. JasonUnsoG表示:

    https://sweetbonanza25.com/# sweet bonanza giris

  11. Wallacefex表示:

    az parayla cok kazandiran slot oyunlar?: slot oyunlar? puf noktalar? – slot casino siteleri

  12. Lorenza表示:

    What’s up, this weekend is nice designed for me, for the reason that this time i am reading this impressive
    informative piece of writing here at my home.

  13. penis表示:

    you are in point of fact a excellent webmaster. The website
    loading pace is incredible. It kind of feels that you are doing any unique trick.
    In addition, The contents are masterwork. you have done a great job in this matter!

  14. Wallacefex表示:

    denemebonusuverensiteler25: deneme bonusu veren siteler yeni – denemebonusuverensiteler25

  15. Doris表示:

    I invested a lot of time in caucus with the two of them, enabling the bile to spill out.

  16. Just had an emergency plumbing situation and learned the importance of regular maintenance Plumbing Grande Prairie

  17. Betty Clark表示:

    Power washing is definitely on my to-do list this spring—can’t wait to see the transformation! Pressure Washing Company

  18. Rosa Freeman表示:

    Thanks for the detailed post. Find more at top-ranked best plastic surgeon

  19. Cryptocurrency fortifies the newest stability of wagering through providing a guaranteed visibility.

  20. Earl Knight表示:

    Just found out that neglecting my roof could lead to serious issues down the line—time to act now before it’s too late! Roof Cleaning Service

  21. FrankEmine表示:

    deneme bonusu veren yeni siteler: yat?r?ms?z deneme bonusu veren siteler – deneme bonusu veren yeni siteler

  22. JasonUnsoG表示:

    https://slotsiteleri25.com/# az parayla cok kazandiran slot oyunlar?

  23. Dannyfut表示:

    drug markets dark web darkmarket url tor markets links

  24. Hi mates, its enormous piece of writing concerning cultureand fully
    explained, keep it up all the time.

  25. Hmm is anyone else having problems with the images on this blog loading?
    I’m trying to find out if its a problem on my end or if it’s the blog.
    Any feedback would be greatly appreciated.

  26. Omnacortil Anti-inflammatoriske Kvalitet Betale, Disakloof Camp Site. Online Order Kamagra Jelly Rx Apotek I Aarhus Hvor Bestiller Generisk Kamagra. Inderal Utan Recept, Apotek Online Sverige, liKolo Carts. Billige Medicin Cialis-professional Juridisk Apotek, SA Motorcycles. Apotek billigste, anmeldelser bivirkninger, en tablet uden recept, efter hvor. Til de laveste priser og granti for tilfredshed. Meloxicam cialis interactions Viagra apotek pris Viagra accoutumance Cialis 35. Danmark uden recept, og det receptpligtige produkt i Danmark er et med. Certified pharmacy online, tablete u beogradu Atsiliepimai apie. https://pilleudenrecept.space/maxalt/ Kob Maxalt Billig

  27. Dannyfut表示:

    tor markets deep dark web tor markets

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。