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,159,700 Responses

  1. Hi there, I desire to subscribe for this website to
    obtain newest updates, therefore where can i do it please assist.

  2. конференц залы оборудование конференц залы оборудование .

  3. My spouse and I stumbled over here from a different web page
    and thought I might check things out. I like what I see
    so i am just following you. Look forward to looking at your web page for a second time.

  4. oscarreys表示:

    Find Out What Demo Hades Tricks The Celebs Are Using oscarreys

  5. It’s Enough! 15 Things About Double Glazing Repair Near Me We’re Sick
    Of Hearing double glazing near me – vocerh.abril.com.br

  6. Wow, amazing blog layout! How lengthy have you ever been blogging
    for? you made running a blog look easy. The whole look of
    your web site is great, let alone the content!

  7. Genuine HGH Benefits (NewULife Hgh Gel Not Tested Yet).

    My web-site; content analysis report example

  8. It’s The Perfect Time To Broaden Your Upvc Repairs Near Me Options Window Repair Near Me

  9. See What Double Glazing Crawley Tricks The Celebs Are Making Use Of double Glazing Crawley

  10. Why You Must Experience Door Fitting Birmingham At
    The Very Least Once In Your Lifetime birmingham specialist glass

  11. e-commerce表示:

    Simply want to say your article is as astonishing.
    The clarity in your submit is simply cool and i could suppose
    you are an expert on this subject. Well with your permission let me to clutch your
    RSS feed to keep updated with impending post.
    Thank you 1,000,000 and please keep up the rewarding work.

  12. You could certainly see your enthusiasm in the work you write.
    The sector hopes for even more passionate writers such
    as you who aren’t afraid to mention how they believe.
    At all times follow your heart.

  13. The vapor and taste top quality is additionally terrific.

    My web site; plant based vape

  14. Please let me know if you’re looking for a author for your weblog.
    You have some really great articles and I think
    I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some articles for your blog in exchange for a link back to mine.
    Please send me an email if interested. Many thanks!

  15. Thanks for finally talking about > JS 依照元素出現數量排序 –
    馬老師 雲端研究室 < Loved it!

  16. Authentic HGH Perks (NewULife Hgh Gel Not Examined Yet).

    Also visit my web page: types of va disability benefits

  17. Hello there! This is kind of off topic but I need some advice from an established blog.
    Is it very hard to set up your own blog? I’m not
    very techincal but I can figure things out pretty quick.
    I’m thinking about setting up my own but I’m not sure where to start.
    Do you have any ideas or suggestions? Cheers

  18. The Reason Why Window Repair Near Is Everyone’s Obsession In 2023 Upvc Windows Repair Near Me

  19. конференц залы оснащение конференц залы оснащение .

  20. Great post! We are linking to this particularly great
    article on our website. Keep up the great writing.

  21. современный конференц зал https://www.oborudovanie-konferenc-zalov11.ru .

  22. конференц-залы под ключ конференц-залы под ключ .

  23. Bookmarks表示:

    Discovering a language does not have to be expensive.

    Also visit my blog :: Bookmarks

  24. Bookmarks表示:

    You do NOT want to consume Lion’s Hair Mushrooms raw.

    my homepage :: Bookmarks

  25. www.N49.com表示:

    See What Glass Repair Leeds Tricks The Celebs Are Utilizing Glass Repair leeds (http://www.N49.com)

  26. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://lacalabrianeifrancobolli.it/uvvqu of type and scrambled it to make a type specimen book.

  27. Nine Things That Your Parent Teach You About Glass Replacement Windows Glass replacement windows

發佈留言

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