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,234,926 Responses

  1. I am extremely impressed with your writing skills and also with the layout
    on your blog. Is this a paid theme or did you customize
    it yourself? Anyway keep up the excellent quality writing, it
    is rare to see a great blog like this one nowadays.

  2. It’s remarkable to pay a visit this web page and reading the
    views of all friends concerning this piece of writing,
    while I am also eager of getting know-how.

  3. rust cheats表示:

    Hmm is anyone else experiencing problems with the images on this blog loading?

    I’m trying to determine if its a problem on my end or if it’s the blog.
    Any suggestions would be greatly appreciated.

  4. 5 Killer Quora Answers On Daftar Situs Togel daftar situs togel

  5. 匿名訪客表示:

    Hey there just wanted to give you a quick heads up.

    The words in your article seem to be running off the screen in Internet explorer.

    I’m not sure if this is a formatting issue or something to
    do with web browser compatibility but I thought I’d post to let you know.
    The layout look great though! Hope you get the problem resolved soon. Thanks

  6. ClydePab表示:

    slot tr online: en cok kazand?ran slot oyunlar? – az parayla cok kazandiran slot oyunlar?

  7. Derekfar表示:

    deneme bonusu veren siteler betturkey betturkey.com: deneme bonusu veren siteler – deneme bonusu veren siteler betturkey betturkey.com

  8. mp3juices表示:

    Woah! I’m really enjoying the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s tough to get that “perfect balance” between user
    friendliness and appearance. I must say you’ve done a excellent job with this.
    Additionally, the blog loads extremely fast for me on Internet explorer.

    Superb Blog!

  9. CarrollUnton表示:

    deneme bonusu veren siteler betturkey betturkey.com https://matadorbet.bid/# matadorbet giris

  10. The Ultimate Glossary Of Terms About Mystery Box online mystery box website

  11. Derekfar表示:

    deneme bonusu veren siteler yerliarama.org: deneme bonusu veren siteler betturkey – deneme bonusu veren siteler betturkey

  12. home deccor表示:

    This is the right blog for everyone who would like to understand this topic.
    You realize so much its almost hard to argue with you (not that I really would want
    to…HaHa). You certainly put a fresh spin on a subject which has been discussed for a
    long time. Great stuff, just great!

  13. Choosing Cat Flap Insulation Cat Flap Insulation (Zenwriting.Net)

  14. Тут можно преобрести сейф под оружие оружейный шкаф

  15. macauslot88表示:

    I got this web site from my friend who told me concerning this web page and
    at the moment this time I am browsing this site and reading very informative
    content at this place.

  16. Тут можно преобрести купить сейф для охотничьего оружия купить сейф для ружья

  17. Derekfar表示:

    Casino Siteleri: casino siteleri win – Casino Siteleri

  18. Why Nobody Cares About Small Fabric Corner Sofa Fabric Corner Sofas

  19. fuck at mall表示:

    Asking questions are genuinely nice thing if you are
    not understanding something entirely, but this post gives
    good understanding even.

  20. Тут можно преобрести купить сейф для ружья в интернет магазине сейф под ружье

  21. ArnHesia表示:

    doesn’t necessarily mean that it’s safe to buy A great way to ivermectin medicine for quality ED drugs. Free shipment waits!

  22. Trefvft表示:

    Вопросы и ответы: можно ли быстро купить диплом старого образца?
    rollexroleplay.moibb.ru/viewtopic.php?f=2&t=249

  23. Тут можно преобрести сейф шкаф купить сейф для оружие

  24. Derekfar表示:

    ultrabet bonus: ultrabet – ultrabet yeni giris 1125

發佈留言

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