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

  1. %%

    My web page … Cs go battle case (bbs.dubu.com.cn)

  2. If you’re looking to securely buy and sell goods or services online,
    consider investing in a verified Cash App account. With its robust
    security features and authentication process, you can have peace of mind while conducting transactions.

    Don’t compromise your safety, opt for a verified Cash App account today.

  3. An intriguing discussion is worth comment. I do believe that you ought to publish more about this topic, it might not be a taboo subject but usually people don’t speak about these issues.
    To the next! Cheers!!

  4. Buy a verified Cash App account and stay worry-free about payment
    transactions. With a verified account, you can securely send and receive
    money, make online purchases, and enjoy various Cash App features hassle-free.
    Don’t wait, get yourself a verified Cash App account today and experience seamless money management.

  5. Nice post. I learn something totally new and challenging
    on blogs I stumbleupon everyday. It’s always helpful to
    read content from other authors and practice
    a little something from their websites.

  6. Buying a verified Cash App account can be a convenient and trustworthy option for those seeking to make secure
    and hassle-free online transactions. With a verified account, users can enjoy benefits like increased transaction limits, easy withdrawal
    options, and enhanced security measures. By purchasing a verified Cash App account, users can ensure a smooth and reliable payment experience,
    making their online transactions a breeze.

  7. If you’re looking to buy a verified Cash App account, look no further!
    A verified account allows you to enjoy all the
    benefits of this popular mobile payment app, providing a secure and convenient
    way to send and receive money. With a verified account, you can enjoy higher transaction limits and added security measures.
    Don’t wait, get your verified Cash App account today!

  8. Thanks designed for sharing such a nice thought,
    paragraph is nice, thats why i have read it entirely

  9. I got this web site from my buddy who shared with
    me about this web site and at the moment this time I am browsing this website and reading very informative content here.

  10. A verified Cash App account ensures a safe and secure
    experience while managing your finances. By purchasing a verified account, you gain access to added features and benefits such
    as higher spending limits and the ability to send and receive money with confidence.
    Don’t compromise your financial security, invest in a verified Cash App account today.

  11. Looking to buy verified Cash App accounts? Look no further!
    Buying a verified Cash App account can provide
    you with added security and peace of mind. With a verified
    account, you can send and receive money without any limitations.
    Don’t miss out on this opportunity, purchase your verified
    Cash App account today!

  12. Piece of writing writing is also a excitement, if you be familiar with afterward you can write if not it is difficult to write.

  13. Margarette表示:

    %%

    Stop by my web blog: double glazing in aldershot – Margarette

  14. Stepanie表示:

    10 . Pinterest Account bean to cup coffee machine with automatic milk frother (Stepanie)
    Be Following About Coffee Machine Beans

  15. As the admin of this web site is working, no question very soon it will be well-known, due to its feature contents.

  16. installers表示:

    %%

    Here is my web-site – installers

  17. Luz表示:

    specializes in asbestos litigation (Luz)

  18. Joseph表示:

    upvc window repairs hitchin (Joseph)

  19. I am regular reader, how are you everybody? This piece of
    writing posted at this site is in fact fastidious.

  20. Mattie表示:

    %%

    Feel free to surf to my web-site … automotive locksmith cost
    (Mattie)

  21. Daniel表示:

    alabama mesothelioma attorney (Daniel)

  22. %%

    Look at my page – csgo case opening websites (milkyway.cs.rpi.edu)

發佈留言

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