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,432,277 Responses

  1. WalterIcone表示:

    How about you?

  2. ZEM表示:

    This web site truly has all the info I needed concerning this subject and didn’t know
    who to ask.

  3. If you’re looking for a hassle-free way to handle your online financial transactions, buying a verified
    Cash App account is a wise choice. With a verified account, you’ll have added security and
    access to additional features that enhance your overall banking
    experience. Don’t waste any more time, invest in a verified
    Cash App account today!

  4. Lavonda表示:

    %%

    Feel free to surf to my homepage – volkswagen key replacement near me; Lavonda,

  5. Just want to say your article is as surprising. The clearness
    to your put up is just nice and that i could suppose you’re knowledgeable
    on this subject. Well along with your permission allow me
    to grab your feed to stay updated with impending post.

    Thank you one million and please continue the rewarding work.

  6. essay helper表示:

    Everything is very open with a really clear explanation of the
    challenges. It was really informative. Your website is
    very helpful. Many thanks for sharing!

  7. angry表示:

    Hеya i am forr the first time here. I came across this
    board and I find It trulyy useful & it helped me oᥙt a lot.
    I hope to give something back and aid otherѕ like you helped me.

  8. Looking to buy a verified Cash App account? With
    a verified account, you can enjoy enhanced security features and unlock higher transaction limits.
    Don’t wait to get started, make your purchases more
    convenient and secure today by purchasing a verified
    Cash App account.

  9. That is very fascinating, You are an overly professional blogger.
    I’ve joined your feed and sit up for in the hunt for more of your fantastic
    post. Also, I have shared your web poker site rankings in my
    social networks

  10. Stacia表示:

    %%

    my webpage: double glazed window aldershot – Stacia,

  11. Asking questions are actually good thing if you are not understanding anything completely, except this post offers good understanding yet.

  12. Jacklyn表示:

    %%

    Also visit my blog upvc doors cambridge (Jacklyn)

  13. Valuable info. Lucky me I found your website by accident, and I am surprised why this coincidence didn’t
    came about earlier! I bookmarked it.

  14. Are you tired of being limited on Cash App? Look
    no further! Buy a verified Cash App account to unlock a world of opportunities.
    With a verified account, you can enjoy higher transaction limits, access to Bitcoin trading, and instant transfers.

    Don’t miss out on the convenience and flexibility.
    Get your verified Cash App account today!

  15. Bethany表示:

    autowatch ghost installers west midlands (Bethany)

  16. Free Software表示:

    Hi there! Quick question that’s completely off topic. Do you know how to make your site
    mobile friendly? My site looks weird when browsing from my apple iphone.
    I’m trying to find a theme or plugin that might
    be able to resolve this problem. If you have any suggestions,
    please share. Thanks!

  17. Introducing the convenience and security of buying
    a verified Cash App account. With authentication measures in place, your
    transactions are protected. Take advantage of this reliable method to
    enhance your financial experience. Safeguard your transactions, buy a verified Cash App account today.

  18. Florrie表示:

    %%

    Feel free to surf to my web site; hertfordshire electricians (Florrie)

  19. Te.Legra.Ph表示:

    %%

    Take a look at my blog post: Organic Cbd Oil – Te.Legra.Ph

  20. sitemap.xml表示:

    I love it when individuals comne tkgether and shqre thoughts.

    Great website, contiue thhe good work!

  21. Are you looking to buy a verified Cash App account? Look no further!
    A verified Cash App account offers enhanced security, higher transaction limits, and more features.
    With a reliable and verified account, you can easily send and receive money, make investments, and even use the Cash App debit card.
    Don’t miss out on the benefits of a verified Cash App account.
    Get yours today!

  22. Rozella Stull表示:

    Amazing all kinds of superb material!

發佈留言

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