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,231,667 Responses

  1. That is very attention-grabbing, You are a very skilled blogger.
    I’ve joined your feed and look ahead to looking for more of your fantastic post.
    Also, I have shared your website in my social networks

  2. I’m impressed, I must say. Rarely do I come across a blog that’s both educative and interesting, and let me tell you, you have
    hit the nail on the head. The issue is something not enough
    men and women are speaking intelligently about.
    I am very happy I came across this in my search for something concerning this.

  3. unlike this version of poker, the seven-card stud originated in https://sugarpromocodes.com/ only in the middle 19th century,
    and was widely distributed by the American military.

  4. Yesterday, while I was at work, my cousin stole my iphone
    and tested to see if it can survive a 25 foot drop, just so she can be
    a youtube sensation. My iPad is now broken and she has 83
    views. I know this is completely off topic but I
    had to share it with someone!

  5. KevinSounc表示:

    http://slotsiteleri.bid/# slot oyunlar? siteleri

  6. Berry表示:

    Way cool! Some very valid points! I appreciate you penning this write-up plus the rest of the site is also very good.

  7. You should take part in a contest for one of the highest
    quality websites on the internet. I am going to recommend this
    website!

  8. KevinSounc表示:

    http://slotsiteleri.bid/# en iyi slot siteleri 2024

  9. Cazrfbs表示:

    Парадокс, но купить диплом кандидата наук оказалось не так и сложно
    lunacharskiy.com/images/pages/?kak_bezopasno_i_legalno_kupit_diplom_v_rf

  10. slot zeus表示:

    Awesome post.

  11. Darrelherse表示:

    deneme bonusu veren slot siteleri: deneme veren slot siteleri – slot kumar siteleri

  12. mega888表示:

    I am sure this piece of writing has touched all the internet visitors,
    its really really nice piece of writing on building up new webpage.

  13. situs toto表示:

    Thiѕ post іs reallʏ a good onee it assists neᴡ the web
    viewers, wһo are wishing іn favor of blogging.

    Also visit my web site situs toto

  14. Darrelherse表示:

    deneme bonusu veren siteler: en yeni slot siteleri – yasal slot siteleri

  15. Way cool! Some extremely valid points! I appreciate you writing this article and also the rest of
    the website is extremely good.

    Also visit my web blog :: https://www.cucumber7.com/

  16. Hey There. I found your blog the use of msn. That is a
    very well written article. I will make sure to bookmark it and return to read more
    of your helpful information. Thank you for the post. I
    will certainly comeback.

  17. Hi there, all the time i used to check blog posts here in the early
    hours in the morning, as i love to gain knowledge of more and more.

    Have a look at my website: is pro nerve 6 a scam

  18. KevinSounc表示:

    https://sweetbonanza.network/# sweet bonanza hilesi

  19. buybest4u表示:

    I have to thank you for the efforts you have put in writing this blog.
    I’m hoping to see the same high-grade content by you in the future as well.
    In truth, your creative writing abilities has inspired me to get my very
    own website now 😉

  20. Профессиональный сервисный центр по ремонту холодильников и морозильных камер.
    Мы предлагаем: ремонт холодильников с выездом
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  21. Darrelherse表示:

    oyun siteleri slot: en iyi slot siteler – bonus veren slot siteleri

  22. Danielle表示:

    What a material of un-ambiguity and preserveness of precious experience on the topic of unpredicted emotions.

  23. Darrelherse表示:

    guvenilir slot siteleri: en guvenilir slot siteleri – slot siteleri

  24. I was wondering if you ever thought of changing the layout of your
    blog? Its very well written; I love what youve got to say.

    But maybe you could a little more in the way of content so people could
    connect with it better. Youve got an awful lot
    of text for only having 1 or 2 images. Maybe
    you could space it out better?

發佈留言

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