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,240,369 Responses

  1. e828611表示:

    A reconstructed engine can easily rejuvenate an automobile, specifically for engines with far better days. Purchasing a rebuilt motor can stretch your vehicle’s lifespan without the cost of a brand-new motor vehicle or a brand-new motor, https://us.enrollbusiness.com/BusinessProfile/6944751/BluePrismAutomotive.

  2. 匿名訪客表示:

    Stunning quest there. What happened after?
    Thanks!

  3. Wow that was odd. I just wrote an incredibly long comment but after
    I clicked submit my comment didn’t show up. Grrrr…

    well I’m not writing all that over again. Regardless, just wanted
    to say superb blog!

  4. Dereketerm表示:

    can you get cheap clomid no prescription: generic clomid – can i buy generic clomid no prescription

  5. Louissuext表示:

    Comprehensive medicine overview. Detailed drug knowledge.
    buy proscar 5 mg
    Dosing guidelines here. Get pill facts.

  6. JeffreyGen表示:

    Read this eye-opening article, recommend you give it a look https://fcinfo.ru/zakon/6984-chto-takoe-eskort-i-v-chem-osobennosti-dannoy-uslugi.html

  7. BrandenSlomy表示:

    buy priligy max pharm: buy priligy – buy priligy max pharm

  8. What’s up everybody, here every one is sharing these
    kinds of familiarity, so it’s pleasant to read this blog,
    and I used to pay a visit this website every day.

  9. I’m truly enjoying the design and layout of your blog. It’s a very easy on the eyes which makes it much more pleasant
    for me to come here and visit more often. Did you hire out a designer to create your
    theme? Great work!

  10. Ionic silver表示:

    Useful information. Fortunate me I discovered your site unintentionally, and I am shocked why
    this coincidence did not took place earlier! I bookmarked it.

  11. CuanToto表示:

    Today, I ѡent to the beach front with my kids.
    I found а sеа shell аnd gaѵe it tto my 4 year old daughter
    and said “You can hear the ocean if you put this to your ear.” Ѕһe plaϲеd thе shell tߋ her ear and screamed.
    Tһere ᴡas a hermit crab іnside and it pinched hher ear.
    She neveг wants to go back! LoL I қnow this is totaly off
    tppic bbut Ӏ haԀ to tel sօmeone!

    Feel free tto surf to my website: CuanToto

  12. Nikefut表示:

    darknet websites darkmarkets https://darknetmarketstore.com/ – dark web websites

  13. I don’t even know how I ended up here, but I thought this post was great.
    I don’t know who you are but definitely you are going to a famous blogger if you are not already 😉
    Cheers!

  14. Casino表示:

    magnificent post, very informative. I’m wondering why
    the other experts of this sector don’t realize this.
    You should proceed your writing. I’m confident, you have a huge readers’ base already!

  15. StevenNub表示:

    priligy: dapoxetine price – buy priligy

  16. The provided content of this blog entry is truly
    captivating . I appreciated the way you scrutinized the various issues
    so comprehensively and unambiguously.
    You assisted me gain innovative viewpoints that I never previously pondered before.
    Thank you for sharing your mastery and expertise –
    it has enabled me to learn even more.
    I specifically enjoyed the novel perspectives you
    showcased , which enlarged my horizons and cognition in meaningful courses.
    This blog is organized and engaging , which is fundamental for subject matter of this caliber .

    I look forward to review further of your compositions in the upcoming period, as
    I’m convinced it is sure to continue to be illuminating and
    facilitate me maintain improving. Thanks again !

    My web-site – online casino minimum deposits

  17. I think the admin of this website is actually working hard for
    his site, for the reason that here every stuff is quality based material.

  18. Whoa plenty of beneficial tips.

  19. Descubra o mundo misterioso do desconhecido.
    Sonha com um cao que morde o seu braco? Doeu e sangrou? Interpretacao de um sonho de acordo com diferentes livros de sonho

    [url=https://www.razboinici.ro/forum/index.php?action=vthread&forum=1&topic=3&page=2372#msg347080]Sonha com um cao que morde o seu braco? Doeu e sangrou? Interpretacao de um sonho de acordo com diferentes livros de sonho[/url] [url=http://thinktoy.net/bbs/board.php?bo_table=customer2&wr_id=855425]Com que e que um el[/url] [url=https://sharonmyphotos.com/2022/08/11/hello-world/#comment-15577]Num sonho, apanhar coisas[/url] [url=https://mercedes-world.com/eq/mercedes-benz-ev-will-come-sooner/comment-page-2291#comment-132354]Porque e que sonho com o kvass?[/url] [url=https://biyoushitsu-oruga.com/hello-world/#comment-712727]Porque e que sonho em apanhar damascos?[/url] 983f1fa

  20. It’s going to be end of mine day, except before finish I am reading this fantastic piece of writing
    to improve my experience.

  21. Jackpot Bet表示:

    Nice blog here! Also your web site loads up fast! What web host are you using?
    Can I get your affiliate link to your host? I
    wish my web site loaded up as quickly as yours lol

    Feel free to surf to my webpage Jackpot Bet

  22. StevenNub表示:

    priligy maxpharm: max pharm – buy priligy

  23. BrandenSlomy表示:

    dapoxetine price: max pharm – dapoxetine price

發佈留言

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