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,667,648 Responses

  1. Togel4D表示:

    9 Lessons Your Parents Teach You About Togel4d Login Togel4D

  2. LeroyBak表示:

    amoxicillin without a doctor’s prescription cheap prescription drugs best natural ed treatment

  3. slot表示:

    Hmm is anyone else experiencing problems with the pictures
    on this blog loading? I’m trying to determine if its
    a problem on my end or if it’s the blog. Any feed-back would
    be greatly appreciated.

  4. Right here is the perfect website for anyone who hopes to understand this
    topic. You understand so much its almost hard to argue with you
    (not that I really would want to…HaHa). You definitely put a new spin on a
    topic which has been discussed for years. Excellent stuff, just
    excellent!

  5. Jorgeagopy表示:

    buying from online mexican pharmacy: mexico pharmacy – mexico drug stores pharmacies

  6. Hello everyone, it’s my first pay a quick visit at this web page, and piece of writing is in fact fruitful in favor
    of me, keep up posting such content.
    https://w1.togelhongkongpools.org/

  7. slot表示:

    My partner and I stumbled over here from a different web page and thought I might as
    well check things out. I like what I see so now i’m following you.

    Look forward to going over your web page again.

  8. Jorgeagopy表示:

    buy erection pills: cheap meds – canadian drug

  9. LeroyBak表示:

    medication for ed cheap meds treating ed

  10. Letha表示:

    You’ll Never Guess This Large 2 Seater Fabric Sofa’s Secrets large 2
    seater fabric sofa [Letha]

  11. RTP duatoto表示:

    I think the admin of this web site is in fact working hard in support of his web site,
    because here every data is quality based stuff.

    My website RTP duatoto

  12. I think this is among the most significant info for me. And i
    am glad reading your article. But wanna remark on few general things, The site style is great, the articles is really great : D.

    Good job, cheers

  13. Jorgeagopy表示:

    reputable indian online pharmacy: Pharmacies in India that ship to USA – indian pharmacy

  14. Woodrow表示:

    10 Things That Your Family Teach You About Automotive Locksmiths Near Me automotive locksmiths near me (Woodrow)

  15. What’s The Point Of Nobody Caring About Sectional Sleeper
    Sofa With Storage sleeper Sectional

  16. Definitely believe that which you said. Your favorite reason appeared to be on the internet the
    simplest factor to take into account of. I say to you, I certainly get annoyed whilst people think about concerns that they just don’t recognize about.
    You managed to hit the nail upon the top and also defined out the whole thing
    without having side-effects , people could take a signal.
    Will probably be back to get more. Thank you

  17. How Adhd Assessment In Adults Is A Secret Life Secret Life Of Adhd Assessment
    In Adults get an adhd Assessment

  18. LeroyBak表示:

    reputable mexican pharmacies online mexico pharmacy cheap buying prescription drugs in mexico

  19. DevonPhack表示:

    https://mexicanpharm24.cheap/# mexican rx online
    pumps for ed

  20. Having read this I thought it was extremely informative.
    I appreciate you finding the time and energy to put this article together.
    I once again find myself personally spending a significant amount of time both reading and leaving comments.
    But so what, it was still worthwhile!

    Review my website: https://www.cucumber7.com/

  21. Pills prescribing information. Generic Name.
    buying cheap paxil without rx
    Actual about medicines. Get now.

  22. On 8 August 2006, Barthez announced he was nonetheless hoping to play professional football for one more two years, insisting he was additionally looking to continue his career in the French worldwide setup.

  23. Guide To Single Pushchair With Buggy Board: The Intermediate
    Guide For Single Pushchair With Buggy Board single pushchair with buggy board (auntnotify0.bravejournal.net)

  24. LeroyBak表示:

    erectional dysfunction cheap medication real viagra without a doctor prescription usa

  25. Thanks for sharing your thoughts on https://www.cucumber7.com/.
    Regards

  26. This is a good tip particularly to those fresh to the blogosphere.
    Simple but very precise information… Many thanks for
    sharing this one. A must read post!

  27. click now表示:

    Hello there, I discovered your web site by means of Google while looking for a similar topic, your web site came up, it looks good.
    I have bookmarked it in my google bookmarks.

    Hello there, simply became aware of your blog through Google,
    and found that it’s truly informative. I’m gonna watch out for brussels.
    I will be grateful if you happen to continue this
    in future. Many people will be benefited out of your writing.
    Cheers!

  28. great points altogether, you just received a new
    reader. What may you recommend about your submit that you made some days in the past?
    Any sure?

發佈留言

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