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,154,968 Responses

  1. eoopxrjda表示:

    Noun. Day light savings. Transylvania. Holocaust. Tyler perry movies. Their eyes were watching god. https://81.200.117.113

  2. gadgets表示:

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

  3. Frankevild表示:

    https://ozempic.art/# ozempic cost

  4. Frankevild表示:

    https://ozempic.art/# ozempic cost

  5. Jeremypag表示:

    semaglutide online semaglutide cost rybelsus pill

  6. Mobility Scooter For Shopping Tools To Streamline Your Daily Lifethe One Mobility Scooter For Shopping Trick Every
    Individual Should Know mobility scooter For Shopping

  7. See What Best Lightweight Folding Electric Wheelchair Uk Tricks The Celebs Are Using folding electric wheelchair uk

  8. যখন আপনি শেষ ব্যক্তিগত অ্যালবাম, আপনার সকলের জন্য অপেক্ষা করছে একটি বিশাল আর্থিক পুরস্কার!

    Check out my web blog: https://parimatch-slots-bd.com/

  9. helpful hints表示:

    Sweet blog! I found it while surfing around on Yahoo News.
    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!

    Thank you

  10. HaroldBig表示:

    buy ozempic: buy cheap ozempic – buy cheap ozempic

  11. What Will Toto Be Like In 100 Years? 먹튀검증 사이트

  12. Jeremypag表示:

    ozempic ozempic online buy cheap ozempic

  13. Politely fellow commentator ,

    I unearthed the reflections shared in this blog post to be incredibly enlightening
    . The writer’s mastery of the subject is genuinely commendable.

    If you are desiring a thrilling and advantageous internet-based gaming adventure, I would sincerely
    convince you to uncover the possibilities of VIVARO CASINO.

    With its vast array of mesmerizing recreations, copious rewards , and hassle-free experience ,
    VIVARO CASINO facilitates an matchless recreational experience that appeals to
    both inexperienced and veteran enthusiasts in the same manner .

    I persuade you to explore VIVARO CASINO and reveal the adrenaline that is
    ready to be experienced you. I am certain you will perceive the
    experience to be highly rewarding.

    Kindest regards

    Here is my website … online gambling

  14. HaroldBig表示:

    rybelsus coupon: cheapest rybelsus pills – semaglutide online

  15. кроме того, вы получите возможность настроить переадресацию таким образом, чтобы прием
    вызовов распределялся между группой менеджеров.

    my homepage: Виртуальный номер для телеграм

  16. Initially, giving on your own a peptide injection at home may seem
    confusing or challenging.

  17. mnnyaxnvz表示:

    Color theory. Genetic testing. Kidney stone. Jonestown massacre. Christian bale movies. Omniscient. https://81.200.117.113

  18. JacobGed表示:

    sports betting bonus codes: These are bonuses and special offers provided by Bet at Home, which may include free bets, deposit matches, or enhanced odds on various sporting events.

發佈留言

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