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,520,897 Responses

  1. Earnest表示:

    30 Inspirational Quotes For Retro Fridge Freezer 50 50 side by side fridge freezer dimensions
    (Earnest)

  2. atavi.com表示:

    9 Lessons Your Parents Teach You About Good Cribs good Cribs (atavi.com)

  3. LeroyBak表示:

    best ed pill cheap meds online ed medications

  4. 17 Reasons Why You Should Ignore Rooms To Go Sofa Sale Cheap Couches for sale under $100

  5. The Ultimate Glossary Of Terms For Pragmatic Free 프라그마틱 무료 슬롯

  6. See What Folding Mobility Scooters For Sale Tricks The Celebs Are
    Using folding mobility scooters for sale

  7. Asbestos Attorneys Strategies That Will Change Your Life Mesothelioma Attorneys

  8. This is really interesting, You’re an overly skilled blogger.

    I’ve joined your rss feed and sit up for in search of more of your excellent post.
    Additionally, I’ve shared your web site in my social networks

    Feel free to visit my website – stem cell therapy

  9. LeroyBak表示:

    errectile dysfunction affordable medication over the counter erectile dysfunction pills

  10. Your means of explaining all in this piece of writing is truly good, every one be able to effortlessly understand it, Thanks a lot https://www.provenexpert.com/it-it/pub023/

  11. 24 Hours To Improving St Louis Accident Lawyers accident attorneys

  12. KevinFiels表示:

    Pill facts here. Medication trends described.
    purchase diflucan online no prescription
    Comprehensive drug guide. Patient medicine guide.

  13. 9 Things Your Parents Taught You About SEO Tools about Seo tools

  14. Green Street Origins CBD Gummies Review Street Origins is a brand that has been gaining popularity
    in the world of CBD products due to their commitment to
    using only the highest quality ingredients.

  15. slot表示:

    It’s difficult to find educated people about this
    topic, however, you seem like you know what you’re talking about!
    Thanks

  16. We are a group of volunteers and opening a new scheme in our
    community. Your website provided us with useful information to work on. You’ve done a formidable job and
    our whole group will be grateful to you.

  17. Hello, I check your blog on a regular basis. Your story-telling style is witty,
    keep it up!

  18. telegra.ph表示:

    The Leading Reasons Why People Achieve In The Replacement Vauxhall Key Cost Industry
    vauxhall astra key replacement (telegra.ph)

  19. Toto4D表示:

    5 Killer Quora Answers On Toto4d Toto4D

  20. Amazon AMI表示:

    Hi there! Would you mind if I share your blog with my zynga group?
    There’s a lot of folks that I think would really enjoy your content.

    Please let me know. Cheers

    My homepage – Amazon AMI

  21. celinetoto表示:

    Quality content is the key to interest the viewers to pay a
    quick visit the web site, that’s what this website is providing.

  22. Howdy very nice website!! Man .. Excellent .. Wonderful ..
    I’ll bookmark your site and take the feeds also? I am satisfied to search out a lot of helpful information right here within the publish, we want
    work out more techniques on this regard, thanks for sharing.
    . . . . .

    my blog post: stem cell treatment

  23. Если требуется грузоперевозки Новосибирск Томск, наш сервис поможет сократить расходы.

  24. Hey I know this is off topic but I was wondering if you knew
    of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and
    was hoping maybe you would have some experience with
    something like this. Please let me know if you
    run into anything. I truly enjoy reading your blog and I look
    forward to your new updates.

    my web blog :: Amazon EC2 Virtual Machine

  25. Rolandofiz表示:

    vavafa

  26. The Three Greatest Moments In Car Accident Lawyers
    History Car Accident Lawyer Best

發佈留言

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