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,630,640 Responses

  1. Компанія Apple також не несе відповідальність за точність або достовірність даних, розміщених на
    веб-сайтах сторонніх виробників.

  2. I always used to study piece of writing in news papers but now as I am a user of net so
    from now I am using net for content, thanks to web.

  3. Blakesmema表示:

    https://webcocktail.ru/ — Откройте для себя идеальные кухонные решения на нашем сайте.

  4. Dereketerm表示:

    dapoxetine online: buy priligy max pharm – dapoxetine price

  5. Very nice post. I just stumbled upon your blog and wished to
    say that I have truly enjoyed surfing around your blog posts.
    In any case I will be subscribing to your feed and
    I hope you write again very soon!

  6. Please let me know if you’re looking for a article writer for
    your site. You have some really good posts
    and I think I would be a good asset. If you ever want to take some of the load off, I’d love
    to write some material for your blog in exchange for a link back to mine.

    Please blast me an e-mail if interested. Regards!

  7. Danieljek表示:

    amoxicillin 500mg price http://priligymaxpharm.com/# buy dapoxetine online

  8. Very energetic blog, I liked that bit. Will there be a part 2?

  9. What’s The Job Market For Peritoneal Mesothelioma Asbestos Professionals?
    Mesothelioma Lawsuit

  10. We are a group of volunteers and starting a new scheme in our community.
    Your site offered us with valuable information to work on. You’ve done an impressive job and our entire community will be thankful to you.

  11. whoah this blog is magnificent i really like reading
    your posts. Stay up the good work! You already know, a lot of persons are searching around for this info, you can aid
    them greatly.

  12. I am in fact delighted to glance at this webpage posts which consists of tons of
    valuable data, thanks for providing these kinds of information.

  13. It’s not my first time to visit this web page, i am visiting this website dailly and get fastidious information from here
    daily.

  14. An interesting discussion is worth comment.
    I think that you need to write more about this topic, it might not be a
    taboo matter but generally folks don’t speak about these subjects.
    To the next! Kind regards!!

  15. US Army表示:

    It’s appropriate time to make a few plans for the future and it’s
    time to be happy. I’ve learn this publish and if
    I may I desire to suggest you some interesting issues
    or suggestions. Perhaps you could write subsequent articles referring to this article.
    I desire to learn more things approximately it!

  16. Bradleygoath表示:

    buying cheap clomid online cheap clomid can you buy generic clomid price

  17. BrandenSlomy表示:

    prednisone canada pharmacy: Prednisone Without Prescription – prednisone 54

  18. Drug information. What side effects?
    where to get cheap rizatriptan pill
    Actual about drugs. Read now.

  19. Bradleygoath表示:

    amoxicillin 500 mg online Amoxicillin buy online amoxicillin buy canada

  20. Hi there, I found your website by way of Google even as searching for a comparable matter,
    your web site came up, it looks great. I’ve bookmarked it in my google bookmarks.

    Hello there, just became aware of your blog via Google, and located
    that it’s really informative. I’m gonna be careful for brussels.

    I will be grateful when you proceed this in future. Lots of
    folks might be benefited out of your writing. Cheers!

  21. My brother suggested I might like this web site.
    He was totally right. This post truly made my day.
    You cann’t imagine simply how much time I had spent
    for this info! Thanks!

  22. I needed to thank you for this great read!! I definitely loved every little bit of it.
    I have you bookmarked to check out new things you post…

  23. Узнай все о варикоцеле слева варикоцеле слева

  24. Bradleygoath表示:

    amoxicillin 500mg buy online canada amoxil com pharm amoxicillin 500 mg tablet price

  25. I don’t know whether it’s just me or if everybody else experiencing problems
    with your website. It seems like some of the written text on your content are
    running off the screen. Can someone else please comment and let me know if this is happening
    to them too? This might be a issue with my browser because I’ve had this happen previously.
    Kudos

  26. Узнай все о варикоцеле 1 степени варикоцеле 3 степени

發佈回覆給「Bitcoin Pulse Trader Erfahrungen」的留言 取消回覆

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