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,482,900 Responses

  1. Markdob表示:

    Приветствую. Подскажите, где найтиразные статьи о недвижимости? Пока нашел – https://teplodom78.ru

  2. AlbertDuM表示:

    clopidogrel bisulfate 75 mg: cheapest plavix – Cost of Plavix on Medicare

  3. rtp slot表示:

    It’s very effortless to find out any topic on net as compared to books, as I
    found this post at this web page.

  4. Markdob表示:

    Приветствую. Может кто знает, где найтиполезные статьи о недвижимости? Пока нашел – https://teplodom78.ru

  5. вывод из запоя в стационаре анонимно http://gov.ukrbb.net/viewtopic.php?f=3&t=6377/ .

  6. Fachai Casino – best slot online in the Philippines
    better than jili slot and pg slot
    free Spins 3000+ Free Demo Slot

    searching fachaicasino.org on Google or Bing to get free coins for play

    https://fachaicasino.org/

  7. mau777表示:

    all the time i used to read smaller articles that as well clear their motive, and that is also happening with this
    post which I am reading at this time.

  8. Hey! Do you know if they make any plugins to safeguard against
    hackers? I’m kinda paranoid about losing everything I’ve worked
    hard on. Any recommendations?

  9. Impressive, what an insightful post! I really delighted in reading
    about your outlook on this topic . It’s absolutely given me
    a wealth to ponder .

    I’d love to hear more of your thoughts , if you’re agreeable
    to extending the dialogue .

    Additionally , have you discovered of MEGA888?
    It’s an wonderful virtual gaming platform with heaps of enthralling options
    . I’ve participated in there and the experience has been exceptional.

    Assuming that you’re in search of a novel way to
    experience some fun and potentially triumph , I’d
    emphatically recommend investigating it further.

    Let me know if you’re interested and I shall provide more details !

    Have a look at my site; mega888 singapore (https://canvas.instructure.com/)

  10. Markdob表示:

    Всем привет! Может кто знает, где найтиразные статьи о недвижимости? Пока нашел – https://teplodom78.ru

  11. nbetcyou表示:

    you are actually a excellent webmaster. The website loading speed is incredible.
    It kind of feels that you’re doing any distinctive trick.
    In addition, The contents are masterwork. you’ve done a fantastic activity
    in this topic!

  12. Markdob表示:

    Приветствую. Может кто знает, где почитатьполезные статьи о недвижимости? Сейчас читаю – https://teplodom78.ru

  13. This is really interesting, You’re a very skilled
    blogger. I have joined your feed and look forward
    to seeking more of your wonderful post. Also, I have shared your web site in my social networks!

    Also visit my web-site … เว็บสล็อตแท้

  14. BrandenSlomy表示:

    prednisone daily use: generic Prednisone – prednisone 20 mg pill

  15. I read this paragraph completely concerning the difference of hottest and earlier technologies, it’s awesome article.

  16. Nikefut表示:

    deep web drug store deep web drug url dark web sites

  17. AlbertDuM表示:

    ivermectin oral solution: generic Stromectol – minocycline ointment

  18. DarrenUnjuh表示:

    https://plavixclo.com/# buy Clopidogrel over the counter

  19. What’s up to every body, it’s my first pay a quick visit of this web site; this web site carries remarkable and actually
    fine data in favor of visitors.

  20. DarrenUnjuh表示:

    http://plavixclo.com/# Plavix 75 mg price

  21. AlbertDuM表示:

    Misoprostol 200 mg buy online: cytpremium – Misoprostol 200 mg buy online

  22. BernardMop表示:

    buy minocycline 50mg otc stromectol nz stromectol ebay

  23. Water fast表示:

    Hey there! I just would like to offer you a big thumbs up for
    your excellent information you’ve got right
    here on this post. I’ll be coming back to your web site for more soon.

  24. bet سایت表示:

    Your way of telling all in this piece of writing is actually
    fastidious, all be capable of without difficulty know it, Thanks a lot.

  25. BernardMop表示:

    п»їcytotec pills online cheapest cytotec Misoprostol 200 mg buy online

  26. I don’t even know how I ended up here, but I thought this post was good.
    I do not know who you are but certainly you are going to a famous blogger if you aren’t already 😉 Cheers!

發佈留言

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