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,521,054 Responses

  1. Magnificent beat ! I wish to apprentice while you amend your
    web site, how can i subscribe for a blog site?
    The account helped me a applicable deal. I were tiny bit familiar of this your broadcast offered bright transparent idea.

    Here is my web site – Sexual Assault lawyers utah

  2. My brother recommended I might like this web site. He was totally
    right. This post actually made my day. You can not imagine just how much
    time I had spent for this info! Thanks!

    My web-site: best sexual harassment lawyers nyc

  3. Richardprurf表示:

    http://pharmaciemeilleurprix.com/# pharmacie en ligne pas cher
    pharmacie en ligne pas cher

  4. No infrared sauna paris timber lasts longer than cedarwood.

  5. I am really loving the theme/design of your website. Do you ever run into any
    browser compatibility issues? A handful of my blog visitors have complained about my website
    not operating correctly in Explorer but looks
    great in Safari. Do you have any tips to help fix this issue?

    my web site best sexual assault lawyers near me

  6. Thanks for every other wonderful article. The place else may anybody get
    that kind of information in such a perfect manner of writing?
    I’ve a presentation subsequent week, and I am at the search for such information.

    My page … sexual misconduct lawyers near me

  7. The most effective CBD products are made from high-quality hemp bacon flavored cbd oil for dogs.

  8. Hey there jᥙat wanted to give youu a quick heqds up.
    The tеxst in your article seem to be running
    off the screedn in Opera. I’m not sure if this is a formatting
    issue οr sometһing to do wiith web browser compatibіlity but I figured I’d pot tto let you know.
    Тhe layout look great though! Hope yooս ցet the proƅlem solved soon.
    Kudos

    My web blog :: kampus terbaik

  9. Hi my loved one! I wish to say that this post is amazing, great written and include
    approximately all vital infos. I would like to look
    extra posts like this .

    Also visit my blog post :: sexual harassment lawyers near me

  10. There’s no need to make monthly payments.

    My page :: equity release calculator natwest

  11. A synthetic USP human development hormonal agent (somatropin).

    Feel free to visit my web-site :: heavy duty truck parts jobs

  12. Exactly what I was looking for, thanks for posting.

    Feel free to visit my website :: sexual harassment Lawyers near me free consultation

  13. I always was concerned types of assault charges in texas this topic and still am, thank you
    for posting.

  14. Real HGH Advantages (NewULife Hgh Gel Not Checked Yet).

    my website; ai detector turnitin

  15. You have little or no mortgage left on the property.

    Feel free to visit my website age partnership owner

  16. They have products to sell or solutions to offer.

    Here is my blog; ai detector for long essays

  17. XYGENYX, a licensing company for FDA-registered items.

    Here is my web blog heavy duty truck parts jobs

  18. Way cool! Some extremely valid points! I appreciate
    you writing this article and the rest of the website is extremely good.

    Here is my web site – sexual assault lawyers philadelphia

  19. Thanks for any other magnificent article. Where else could anyone get that type of information in such an ideal
    method of writing? I’ve a presentation next week, and I’m on the look for such information.

    my webpage; sexual assault defense lawyers near me

  20. Way cool! Some extremely valid points! I appreciate you penning this post plus the rest of
    the site is very good.

    My homepage sexual assault lawyers utah

發佈留言

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