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,230,559 Responses

  1. CarrollUnton表示:

    denemebonusuverensiteler.top https://ultrabet-tr.online/# ultrabet

  2. Hi there would you mind letting me know which
    webhost you’re utilizing? I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot faster then most.

    Can you recommend a good web hosting provider at a honest
    price? Thanks, I appreciate it!

  3. Sazrhib表示:

    Вопросы и ответы: можно ли быстро купить диплом старого образца?
    politikforum.ru/member.php?u=13385920

  4. Car Boot Mobility Scooters Tips To Relax Your Daily Life Car Boot Mobility Scooters Trick That Every Person Must Be
    Able To boot mobility scooters

  5. CarrollUnton表示:

    deneme bonusu veren siteler denemebonusu2026.com https://slot-tr.online/# en cok kazand?ran slot oyunlar?

  6. I blog quite often and I truly appreciate your
    content. Your article has really peaked my interest. I
    will take a note of your blog and keep checking for new details about once a week.
    I opted in for your RSS feed too.

  7. Казахстанский VPS хостинг для стабильной работы сайта дешевый vps в казахстане

  8. Medicine information leaflet. Generic Name.
    lisinopril 40 mg generic
    Some information about medicament. Get here.

  9. Derekfar表示:

    deneme bonusu veren yeni siteler: deneme bonusu veren siteler – deneme bonusu veren siteler denemebonusu2026.com

  10. Sazrlfk表示:

    Полезная информация как официально купить диплом о высшем образовании

    cslregister.com/forum/member.php?u=6225

  11. ClydePab表示:

    deneme bonusu veren siteler betturkey betturkey.com: deneme bonusu veren siteler – deneme bonusu veren siteler yeni

  12. The Infrequently Known Benefits To Private Assessment For
    ADHD adhd Assessment private cost

  13. Diplomi_zpMl表示:

    диплом кандидата наук нового образца купить many-diplom77.ru .

  14. Demi表示:

    5 Must-Know-How-To Toto Website Official Methods To 2024 토토사이트
    추천 (Demi)

  15. Derekfar表示:

    ultrabet tr online: ultrabet yeni giris 1125 – ultrabet giris

  16. Remarkable things here. I’m very glad to see your post. Thank you so much and I’m
    having a look ahead to touch you. Will you kindly drop me a
    mail?

  17. Paragraph writing is also a fun, if you be acquainted
    with afterward you can write if not it is complicated to write.

  18. Derekfar表示:

    deneme bonusu veren siteler mycbet.com: deneme bonusu veren yeni siteler – deneme bonusu veren siteler denemebonusu2026.com

  19. I was curious if you ever considered changing the page layout of your site?
    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 one or two images. Maybe you could space it out better?

  20. Тут можно преобрести сейф противопожарный купить сейф огнестойкий в москве

  21. Diplomi_cmMl表示:

    купить диплом проф переподготовка many-diplom77.ru .

  22. bookkeeping表示:

    Woah! I’m really enjoying the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s challenging to get that “perfect balance” between user friendliness
    and visual appeal. I must say you’ve done a superb job with
    this. Also, the blog loads super quick for me on Firefox.
    Superb Blog!

  23. I’ve been browsing online more than three hours nowadays, yet I never found any fascinating article like
    yours. It is beautiful value sufficient for me. Personally, if all site owners
    and bloggers made just right content as you did, the internet
    will likely be much more helpful than ever before.

  24. Adrianne206表示:

    We are tied directly into the sate’s renewal database which allows us to process your request almost instantly

  25. Live Sydney表示:

    This is very interesting, You’re a very skilled blogger.
    I’ve joined your feed and look forward to seeking more of your fantastic post.
    Also, I’ve shared your website in my social networks!

    https://w9.mbahsemar.org/

  26. car locksmith表示:

    Link exchange is nothing else however it is only
    placing the other person’s website link on your page at proper place and
    other person will also do same for you.

  27. LarryCoell表示:

    slot oyunlar? az parayla cok kazandiran slot oyunlar? az parayla cok kazandiran slot oyunlar?

  28. Hello to all, how is everything, I think every one is getting more from this web page,
    and your views are fastidious in favor of new viewers.

發佈留言

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