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,218,466 Responses

  1. Hello, its nice piece of writing on the topic of media print, we all understand media is a wonderful source of data.

    Look at my page :: Dallas commercial chimney cleaning

  2. Drstobeese表示:

    klonopin online pharmacy: omeprazole uk pharmacy – student store pharmacy hours

  3. See What Double Glazing Repairers Tricks The Celebs Are Utilizing double glazing repairers

  4. Joie表示:

    10-Pinterest Accounts You Should Follow Car Boot Scooter boot scooters for sale (Joie)

  5. The History Of Lightweight 3 Wheel Rollator In 10 Milestones Three wheeled rollators

  6. Five People You Should Know In The Lost Honda Car Key No
    Spare Industry Replacing Honda Key Fob

  7. terima kasih JS 依照元素出現數量排序 – 馬老師 雲端研究室
    karena saya jadi horny dan ingin nonton film tentang perkosa anak kecil sekarang.
    Saya senang nonton video child porn

  8. See What Situstoto Slot Tricks The Celebs Are Using situstoto slot

  9. BOMSLOT表示:

    Very good post. I will be going through some of these issues as well..

  10. 7 Small Changes You Can Make That’ll Make An Enormous Difference To Your ADHD Private Diagnosis Cost UK Private ADHD assessment Bristol Cost

  11. Key Lost car表示:

    9 Things Your Parents Taught You About Key Lost Car
    Key Lost car

  12. casino gcash表示:

    Oh my goodness! Awesome article dude! Many thanks, However I am going through problems with your
    RSS. I don’t understand why I am unable to join it.

    Is there anybody else having the same RSS problems?

    Anyone who knows the answer can you kindly respond?
    Thanks!!

    Have a look at my website – casino gcash

  13. Drstobeese表示:

    Aebgjoync: adipex p online pharmacy – price of cialis at pharmacy

  14. Easydrorbix表示:

    us online pharmacy reviews: Benemid – us online pharmacy reviews

  15. Sheri表示:

    15 Top Twitter Accounts To Discover More About Pvc Window Hinges upvc
    window hinges replacement (Sheri)

  16. See What Double Glazing Repairers Tricks The Celebs Are Using Double Glazing Repairers

  17. Drstobeese表示:

    kmart pharmacy store hours: online pharmacy viagra usa – android app store pharmacy

  18. Easydrorbix表示:

    homeopathic pharmacy online: reputable online pharmacy cialis – viagra prices by pharmacy

  19. Drstobeese表示:

    online pharmacy lortab no prescription: india pharmacy percocet – online pharmacy zovirax cream

  20. Nadużycie表示:

    Fucked out of my knickers.
    One day I was travelling with my brother Artyom in the car.
    He’s a year older than me. We were following his
    friends to celebrate his driving licence. The party was going to be at the dacha.
    Artem told me to sit in the back seat when his three friends got in. I was sandwiched between two
    of them.

    My webpage … Nadużycie

  21. konteyner表示:

    konteyner | Bu makale sayesinde birçok soruma cevap buldum. Teşekkür ederim!

  22. foot care表示:

    My brother suggested I might like this web site.
    He was totally right. This post truly made my day.
    You can not imagine simply how much time I had spent
    for this info! Thanks!

  23. Easydrorbix表示:

    target pharmacy ventolin: ingles pharmacy – kroger pharmacy store locator

  24. JasonItato表示:

    И что в результате?
    task about whether strictly visual mods are allowed in gta online, was the first of those, to who even on-site service rockstar for a long time could not|had the opportunity} to respond during the entire https://fivemods.io/.

  25. Drstobeese表示:

    pharmacy viagra: cheap rx – cialis uk pharmacy

  26. konteyner表示:

    konteyner fiyatları | Yazınız gerçekten profesyonel bir seviyede hazırlanmış ve birçok detaya değinmiş. Konuya dair öğrenmek istediğim her şeyi buldum, emeğinize sağlık!

  27. Easydrorbix表示:

    Malegra DXT plus: percocet mexico pharmacy – how much is cialis at the pharmacy

  28. Hello friends, good paragraph and nice urging commented here, I am genuinely enjoying by these.

    my page: 무료스포츠중계

  29. Amazing blog! Do you have any recommendations for aspiring writers?
    I’m planning to start my own website soon but I’m a little
    lost on everything. Would you advise starting with a free platform like
    Wordpress or go for a paid option? There are so many options out there that I’m completely overwhelmed
    .. Any recommendations? Appreciate it!

發佈留言

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