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,537,616 Responses

  1. George表示:

    Enough Already! 15 Things About Best UK SEO Software We’re Tired
    Of Hearing tool for seo agency, George,

  2. Appreciate this post. Will try it out.

  3. Wallacefex表示:

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

  4. Skup nieruchomości to idealna opcja dla tych, którzy potrzebują natychmiastowej gotówki za swoją nieruchomość. Dzięki temu procesowi można zaoszczędzić czas na poszukiwanie kupca i negocjacje cenowe skup mieszkań Warszawa

  5. Wallacefex表示:

    Casino Siteleri: Deneme Bonusu Veren Siteler – en guvenilir casino siteleri

  6. porno表示:

    It’s an awesome paragraph designed for all the internet people; they will get advantage from
    it I am sure.

  7. Wow, this paragraph is pleasant, my younger sister is analyzing these things,
    therefore I am going to tell her.

  8. Thanks for the insightful write-up. More like this at fire damage restoration services

  9. BradleyEdino表示:

    slot oyunlar? puf noktalar? guvenilir slot siteleri slot oyunlar? puf noktalar?

  10. What Is Tilt And Turn Windows Cost And How To Use It Tilt and turn windows how to open

  11. Graciela表示:

    Great article! We are linking to this particularly
    great content on our site. Keep up the good writing.

  12. JasonUnsoG表示:

    https://denemebonusuverensiteler25.com/# yeni deneme bonusu veren siteler

  13. Katie Rodgers表示:

    My posture has improved dramatically since I started seeing a ##Puyallup Chiropractor## https://list.ly/tothieqepf

  14. kill表示:

    After I originally left a comment I appear to have
    clicked on the -Notify me when new comments are added- checkbox and from now on each time
    a comment is added I receive 4 emails with the same comment.
    There has to be an easy method you are able to remove me from that
    service? Appreciate it!

  15. JasonUnsoG表示:

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

  16. slot toto表示:

    Hi there! Tһis post could not bee written much better!
    Looking at this post reminds me of my previous rоommate!
    He constantⅼy kept talking aboout this. I will ѕеnd thi ρost
    to him. Fairly certain he’s going to havfe a very good read.
    I appreciate you for sharing!

    my wweƄ page: slot toto

  17. Bryan Summers表示:

    Nabywanie kamieni szlachetnych i diamentów: Jak odszukać najlepsze złoto skup

  18. Bernie表示:

    Good day! I could have sworn I’ve been to this blog before
    but after checking through some of the post I realized it’s new to
    me. Nonetheless, I’m definitely glad I found it and I’ll be book-marking and checking back frequently!

  19. Nam表示:

    Incredible points. Solid arguments. Keep up the amazing effort.

  20. Greetings! I know this is kinda off topic but I was wondering if
    you knew where I could get a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having trouble finding one?
    Thanks a lot!

  21. Nelle Gross表示:

    Your discussion on weatherproofing techniques was very helpful! Learn more strategies at CR3 American Exteriors

  22. Lakesha表示:

    I like the valuable info you provide in your articles.

    I’ll bookmark your blog and check again here regularly.

    I’m quite certain I will learn many new stuff right here!
    Best of luck for the next!

  23. Wallacefex表示:

    Casino Siteleri: Casino Siteleri – casino bahis siteleri

  24. Lillie Norris表示:

    This was quite helpful. For more, visit Shelbyville roofers

  25. T-shirt hanging on the hallway clothes rack I cross within the dim light of the early hours.

  26. How To Get Money By Playing Games – Making A Lot Funds In The Casino 또봇 y 에볼루션

  27. FrankEmine表示:

    deneme bonusu veren casino siteleri: yeni bonus veren bahis siteleri – Canl? Casino Siteleri

  28. 부달表示:

    Entertainment 부달

發佈留言

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