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,233,908 Responses

  1. Derekfar表示:

    en cok kazand?ran slot oyunlar?: az parayla cok kazandiran slot oyunlar? – slot tr online

  2. Отличный сайт! Всем рекомендую!шкафы купе на заказ москва

  3. Aus diesem Grund haben wir YouTube Video Downloader
    entwickelt.

  4. LarryCoell表示:

    en cok kazand?ran slot oyunlar? slot oyunlar? puf noktalar? az parayla cok kazandiran slot oyunlar?

  5. MelvinSat表示:

    айфон про 512 цена iphone 128gb pro купить

  6. The Most Effective Reasons For People To Succeed In The
    Case Opening Battle Industry Csgo case battle

  7. Have you ever considered about adding a little bit more than just your articles?

    I mean, what you say is valuable and everything.
    But think of if you added some great pictures or
    video clips to give your posts more, “pop”! Your content
    is excellent but with images and video clips, this website could undeniably
    be one of the most beneficial in its niche. Amazing blog!

  8. Good day! I simply wish to offer you a big thumbs up
    for your excellent information you have got right here on this post.
    I am coming back to your web site for more soon.

  9. Press releases are essential for spreading news about a company’s endeavors.

    They allow media outlets to cover the significant happenings.
    A well-crafted press release is likely to catch
    the eye of journalists, resulting in valuable press attention.

    Additionally, news briefs act as an official record of information, which press agencies trust.
    By crafting frequent Press distribution services Illinois [https://www.santagrand.com/user/profile/149170] communications,
    corporations boost their presence within the media landscape,
    building media goodwill while establishing partnerships
    with news organizations.

  10. link表示:

    I do consider all of the concepts you have offered for
    your post. They are very convincing and can certainly work.
    Nonetheless, the posts are too quick for starters.
    May just you please lengthen them a little from subsequent time?
    Thank you for the post.

  11. Thanks , I have just been searching for information approximately this topic for a while
    and yours is the best I have found out till now.
    However, what concerning the conclusion? Are you positive in regards to the supply?

  12. Thanks for sharing your thoughts on Techtunes Auto. Regards

  13. Trefztg表示:

    Покупка диплома о среднем полном образовании: как избежать мошенничества?

    good-serial.ru/2024/10/diplom-po-lyuboy-spetsialnosti-kak-kupit

  14. Troy表示:

    15 Reasons Why You Shouldn’t Overlook Wall Mount Electric Fireplaces wall
    electric fires – Troy,

  15. I for all time emailed this weblog post page to all my contacts,
    because if like to read it afterward my friends will too.

  16. DennisDem表示:

    красная поляна прокат горнолыжного снаряжения цена аренда лыжи сочи

  17. chip表示:

    Does Technology Make Fiat 500 Replacement Key Cost Better Or
    Worse? chip

  18. Derekfar表示:

    slot tr online: slot oyunlar? puf noktalar? – en cok kazand?ran slot oyunlar?

  19. HassanbuifS表示:

    http://matadorbet.bid/# matadorbet
    denemebonusuverensiteler.top

  20. This is a topic that’s close to my heart… Best wishes! Where are your contact details though?

  21. 14 Clever Ways To Spend The Leftover Wall Fireplace Electric Budget Tabletop Fireplace

  22. The Leading Reasons Why People Perform Well
    On The Online Mystery Box Industry online mystery box websites

  23. LarryCoell表示:

    guvenilir casino siteleri Deneme Bonusu Veren Siteler Canl? Casino Siteleri

  24. Medication information for patients. Drug Class.
    buying generic esomeprazole without rx
    Best what you want to know about medicine. Read information now.

發佈留言

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