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,431,774 Responses

  1. Keys表示:

    %%

    Look at my blog post … Keys

  2. teach表示:

    I’m not that much of a internet reader to be honest but your blogs really nice, keep
    it up! I’ll go ahead and bookmark your site to come back later on. Many thanks

  3. Von表示:

    upvc window repairs gravesend, Von,

  4. Așadar, asigură-te că dispozitivul tău are suficient spațiu, că ai o placă video eficientă sau un procesor puternic pentru videoclipurile mai lungi, dacă e cazul.

  5. Thank you for another excellent post. Where else may anybody get that type of information in such a perfect manner of writing?
    I’ve a presentation subsequent week, and I am on the look
    for such information.

  6. xem sex表示:

    My brother recommended I might like this website. He was totally right.
    This post actually made my day. You can not imagine simply how much time I had spent for this information!
    Thanks!

  7. Hey There. I discovered your weblog the use of msn. That is a very smartly written article.
    I will make sure to bookmark it and return to learn more
    of your useful info. Thank you for the post. I’ll certainly comeback.

  8. What a information of un-ambiguity and preserveness of precious know-how
    on the topic of unexpected feelings.

  9. Rosemarie表示:

    window fitters bexley (Rosemarie)

  10. website表示:

    Hello everyone, it’s my first pay a quick visit at this site, and article
    is actually fruitful designed for me, keep up posting these types
    of articles.

  11. %%

    Take a look at my web blog – what is a bean to cup coffee machine (https://best-bean-To-cup-coffee-m44937.wssblogs.com)

  12. Buy a verified Cash App account for easy and hassle-free
    transactions. With a verified account, enjoy increased security, higher transaction limits, and the ability to link your bank account.
    Don’t wait, get your Cash App account verified today and experience seamless money
    transfers right at your fingertips!

  13. %%

    My blog post :: jon zherka instagram (patrickandmonica.Net)

  14. best taxi表示:

    Thanks , I have recently been searching for information about this
    topic for ages and yours is the greatest I’ve came upon so far.
    However, what in regards to the bottom line? Are you sure concerning the source?

  15. %%

    Also visit my web-site: replacement toyota Key fob – redstudio.co.kr,

  16. testing表示:

    %%

    Stop by my web site: testing

  17. te.Legra.ph表示:

    railroad knee injury settlements (te.Legra.ph)

  18. %%

    Check out my homepage – Siemens EQ900 Fully Automatic Bean to Cup Coffee Machine (simenseq90059546.jts-blog.com)

  19. Everything is very open with a precise explanation of the issues.
    It was definitely informative. Your website is extremely helpful.
    Many thanks for sharing!

  20. Very good blog! Do you have any recommendations for aspiring writers?
    I’m hoping to start my own site soon but I’m a little lost on everything.
    Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many options out there
    that I’m totally confused .. Any tips? Appreciate it!

  21. Are you tired of the hassle of setting up a Cash App
    account from scratch? Look no further! Now you can buy a verified Cash App account and save yourself from the lengthy verification process.
    With a verified account, you can enjoy all the benefits of Cash App without
    any limitations. Don’t waste your time anymore, get a verified Cash App account
    today and start sending or receiving money hassle-free!

  22. Frank表示:

    %%

    Here is my blog premium oil meaning (Frank)

發佈留言

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