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,478,099 Responses

  1. BrettTIT表示:

    Viagra Tablet price: FastPillsForMen – Sildenafil Citrate Tablets 100mg

  2. I am truly thankful to the holder of this web site who has shared
    this enormous article at at this time.

  3. Donaldabula表示:

    cheap ed drugs FastPillsEasy ed pills cheap

  4. Donaldabula表示:

    buy cialis pill buy cialis pill Generic Cialis price

  5. Charlesbot表示:

    http://fastpillsformen.com/# Sildenafil Citrate Tablets 100mg

  6. BrettTIT表示:

    Cheap Cialis: Max Pills For Men – cialis for sale

  7. slotgacor表示:

    My brother suggested I might like this web site. He was entirely right.

    This post truly made my day. You cann’t imagine just how much time I had spent for this info!
    Thanks!

  8. вызов нарколога на дом частная скорая помощь вызов нарколога на дом частная скорая помощь .

  9. OP表示:

    Swedish Massage OP

  10. 789club表示:

    Hi to every one, for the reason that I am truly eager of reading this
    web site’s post to be updated on a regular
    basis. It includes good material.

  11. Amazing piece! The way you handled this topic is remarkably innovative.
    I’ve dedicated time considering this issue for some time, and your insights resonated with
    me.
    How you broke down the core elements was brilliant.
    I specifically appreciated your analysis of the wider impact.

    Looking forward to your next piece! Your perspective keeps delivering valuable insights.
    Keep sharing such insightful pieces!
    I’ll definitely pass this along with my network. This deserves broader exposure!
    Much appreciated for this excellent article!

    Check out my homepage :: Chicago Press (chicagomedia16.werite.net)

  12. неотложная наркологическая помощь в москве неотложная наркологическая помощь в москве .

  13. срочная наркологическая помощь в москве http://klin.0pk.me/viewtopic.php?id=4428 .

  14. Donaldabula表示:

    best price for viagra 100mg cheap viagra over the counter sildenafil

  15. Charlesbot表示:

    http://fastpillseasy.com/# online ed pharmacy

  16. BrettTIT表示:

    where can i get ed pills: FastPillsEasy – online erectile dysfunction prescription

  17. срочное оформление загранпаспорта срочное оформление загранпаспорта .

  18. Jacobwaync表示:

    The 1win promo code: MAX500WIN. This 1Win bonus code 2025 rewards new players with a 500% bonus up to $1025. Valid codes for receiving bonuses without a deposit for new and old users. Hurry to get them today! Activate the promo code 1win and get a generous welcome bonus of up to 500% on your first four deposits.

    1win bonus code today no deposit

  19. 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트
    카지노사이트

  20. Porn Sex表示:

    I relish, result in I found just what I was having
    a look for. You have ended my four day long hunt!
    God Bless you man. Have a great day. Bye

發佈留言

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