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,363,540 Responses

  1. Personal injuries can have lasting effects on your life. It’s vital to have a strong legal ally. For more insights, take a look at Giddens Law Firm

  2. Asking questions are truly fastidious thing if
    you are not understanding something totally, however this post gives nice understanding even.

  3. DanielAbice表示:

    http://cipharmdelivery.com/# buy generic ciprofloxacin

  4. iconwin表示:

    Gooԁ day! Do you know іf they mɑke any plugins to assist with SEO?
    I’m trrying to ցet my blog tto гank foг ѕoe targeted keywords bᥙt I’m not ѕeeing very good gаins.
    If you know oof anyу plesse share. Thank you!

    Feeel free to ѵisit my webpage; iconwin

  5. This paragraph will help the internet users for building
    up new weblog or even a blog from start to end.

    Also visit my web site specialized Alzheimer’s care

  6. Nikefut表示:

    dark markets dark web market dark internet

  7. sga123表示:

    Thanks for the good writeup. It actually was once a enjoyment account it.
    Glance complicated to far brought agreeable from you!
    By the way, how can we be in contact?

  8. Discover More表示:

    Right here is the perfect website for anybody who hopes to find out about this topic.
    You realize a whole lot its almost hard to argue with you (not that
    I actually will need to…HaHa). You definitely put a fresh
    spin on a topic that’s been discussed for years.
    Excellent stuff, just excellent!

  9. DanielAbice表示:

    https://amoxstar.com/# buy amoxicillin online uk

  10. DanielAbice表示:

    http://cipharmdelivery.com/# cipro for sale

  11. I don’t even understand how I ended up right here, however I believed this
    post was great. I do not recognize who you are however certainly you are going
    to a well-known blogger in case you are not already.
    Cheers! https://Sugardaddyschile.cl/question/kit-de-reparation-par-plancher-en-bois-franc-conseils-pratiques/

  12. Idesys.co.kr表示:

    Hi there i am kavin, its my first occasion to commenting
    anyplace, when i read this post i thought i could also create comment due to this
    brilliant paragraph. http://Idesys.co.kr/bbs/board.php?bo_table=free&wr_id=42703

  13. My relatives all the time say that I am wasting my time here at web, but
    I know I am getting know-how every day by reading such nice posts. https://classifieds.ocala-news.com/author/maisiekitam

  14. Live SGP 4D表示:

    Hi mates, its enormous article concerning tutoringand fully explained, keep it up all the time.

  15. KennethPah表示:

    prednisone 20mg prices how much is prednisone 5mg prednisone 10 tablet

  16. RobertJar表示:

    order amoxicillin uk: Amox Star – where can i buy amoxicillin over the counter uk

  17. MoToJIc.COM表示:

    Outstanding quest there. What occurred after? Take care! http://Motojic.com/bbs/board.php?bo_table=free&wr_id=301731

  18. KennethPah表示:

    ciprofloxacin generic ci pharm delivery ciprofloxacin 500mg buy online

  19. SamuelBix表示:

    get cheap clomid price: clomid on pharm – how can i get clomid without insurance

  20. SamuelBix表示:

    cost of generic clomid price: where can i get cheap clomid without prescription – where buy clomid online

  21. DanielAbice表示:

    https://amoxstar.com/# amoxicillin 500 mg price

  22. DanielAbice表示:

    http://cipharmdelivery.com/# where can i buy cipro online

  23. These plumbing tips are lifesavers! I wish I had known them sooner. Visit Gas plumbing for more helpful advice

  24. 9 Things Your Parents Taught You About Capsule Machine Coffee capsule Machine coffee

  25. "Free Gift"表示:

    һi!,I ealⅼү likee youyr wrditing ver much!
    propⲟrgion wee kеdep inn tohch etra alproximately yohr article
    oon AOL? I requikre a speialist inn thhis arfea tto սnravell myy рroblem.
    Maay bbe that’s you! Havingg a ⅼlook ahad too
    llok you.

    Loook nto myy ste – “Free Gift”

  26. Lewis Lane表示:

    This was a great article. Check out live casinos features for more

  27. I absolutely love your blog and find most of your post’s to be just what I’m looking
    for. Do you offer guest writers to write content
    for yourself? I wouldn’t mind writing a post or elaborating on many of the subjects
    you write about here. Again, awesome site!

發佈留言

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