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,467,324 Responses

  1. Masterslot69表示:

    Hi, I check your blogs on a regular basis. Your writing style is
    awesome, keep doing what you’re doing!

  2. Great tips! For more, visit رنگ ضد حریق

  3. FrankEmine表示:

    en cok kazand?ran slot oyunlar?: en cok kazand?ran slot oyunlar? – slot oyunlar?

  4. FrankEmine表示:

    Casino Siteleri: Casino Siteleri – deneme bonusu veren casino siteleri

  5. Nice blog here! Also your site rather a lot up fast!
    What web host are you the usage of? Can I am getting your affiliate link on your host?
    I desire my website loaded up as quickly as yours lol

  6. 화명동에서 저희만큼 돈 많이 버는 곳 없어요~ 화명동 노래방알바에서 돈 좀 벌어 보실래요?

  7. JasonUnsoG表示:

    https://casinositeleri25.com/# casino bahis siteleri

  8. SyjjPaish表示:

    Know more about ED at mesalamine-induced acute intolerance syndrome at competitive prices when you purchase from known online

  9. FrankEmine表示:

    slot siteleri: slot siteleri – en kazancl? slot oyunlar?

  10. Kazuko表示:

    I truly love your website.. Great colors & theme.
    Did you build this amazing site yourself? Please reply back as I’m trying to create my
    own site and want to find out where you got this from or what the
    theme is named. Thank you!

  11. prod表示:

    I don’t even know the way I finished up here, however
    I believed this put up was once good. I do not know who you are however definitely you are going to a
    famous blogger in case you are not already. Cheers!

  12. BradleyEdino表示:

    slot siteleri slot siteleri slot oyunlar?

  13. Edgar Ruiz表示:

    This was highly educational. For more, visit roof repair Mechanicsville

  14. LK21 HD表示:

    WOW just what I was looking for. Came here by searching for LK21
    HD

  15. Ricardo表示:

    Right now it looks like Expression Engine is the preferred blogging platform available right now.
    (from what I’ve read) Is that what you are using on your blog?

  16. Leigh表示:

    I am actually happy to read this website posts which carries plenty of
    useful information, thanks for providing these statistics.

  17. JasonUnsoG表示:

    http://slotsiteleri25.com/# slot casino siteleri

  18. BradleyEdino表示:

    sweet bonanza oyna sweet bonanza sweet bonanza oyna

  19. Buck表示:

    Pretty! This has been an incredibly wonderful article.

    Thank you for providing this info.

  20. Oh my goodness! Amazing article dude! Thank you so much, However
    I am encountering troubles with your RSS. I don’t understand why I cannot subscribe to it.
    Is there anyone else having identical RSS problems? Anybody who
    knows the solution will you kindly respond? Thanks!!

    Visit my website; best decaf tea

  21. BOKEP TERBARU表示:

    This is a topic which is near to my heart… Cheers!
    Exactly where are your contact details though?

  22. I get pleasure from, result in I found exactly what I used
    to be taking a look for. You’ve ended my 4 day long hunt!

    God Bless you man. Have a great day. Bye

  23. Hi to every single one, it’s really a fastidious for me to go
    to see this website, it consists of helpful Information.

  24. Mercedes表示:

    Hello to all, how is everything, I think every one is getting more from this web site, and your views are nice designed for
    new users.

  25. Its like you read my mind! You seem to know a lot approximately this, such as you wrote the book in it or something.
    I think that you simply could do with a few p.c. to drive the message house a bit, however other than that, that is wonderful blog.
    An excellent read. I’ll certainly be back.

  26. Wallacefex表示:

    en cok kazand?ran slot oyunlar?: slot siteleri – en cok kazand?ran slot oyunlar?

  27. PeterAlact表示:

    Займы в Казахстане онлайн — быстрое оформление, минимальные требования, выгодные условия для получения нужной суммы микрокредиты онлайн

  28. https://digi116sa.z31.web.core.windows.net/research/digi116sa-(363).html
    Floral prints and delicate colors play well with decor that is sure to embrace the blooms of the spring and summer time months.

發佈留言

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