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,373,169 Responses

  1. электрические карнизы для штор электрические карнизы для штор .

  2. BrandenSlomy表示:

    prednisone 5 mg tablet cost: prednisoneraypharm – prednisone 10mg prices

  3. StevenNub表示:

    priligy maxpharm: priligy maxpharm – buy priligy max pharm

  4. Beverly表示:

    Have you ever considered about including a little bit more than just your articles?
    I mean, what you say is important and all. But think of if
    you added some great graphics or videos to give your posts
    more, “pop”! Your content is excellent but with images and videos, this website could certainly be one of the
    best in its field. Wonderful blog!

  5. StevenNub表示:

    buy amoxicillin canada: amoxil com pharm – cheap amoxicillin 500mg

  6. Hello, all the time i used to check blog posts here early
    in the break of day, because i enjoy to find out more and
    more.

  7. Two-factor authentication – additional verification of the login with codes
    sent to your device or email address at the https://johnvegascasinos.com.
    Peripheral devices do not overlook the mouse, keyboard, headphones and other accessories.

  8. нарколог на дом в краснодаре http://www.ya.7bb.ru/viewtopic.php?id=14601/ .

  9. карниз для штор с электроприводом карниз для штор с электроприводом .

  10. bu cu xx表示:

    Hi, after reading this remarkable piece of writing i
    am too happy to share my experience here with friends.

  11. BrandenSlomy表示:

    price of amoxicillin without insurance: com pharm – amoxicillin 500mg no prescription

  12. Blakesmema表示:

    http://www.tort-dubr.ru — Все о кухнях на заказ на нашем сайте.

  13. There are various tools and websites that claim to permit users to view private Instagram profiles, but it’s important
    to open these gone caution. Many of these tools can be unreliable, may require personal information, or could violate Instagram’s terms of service.
    Additionally, using such tools can compromise your own security or guide to scams.
    The safest and most ethical way to view private instagram free a private
    profile is to send a follow request directly to the user.
    Always prioritize privacy and respect in your online interactions.

  14. Nice blog here! Also your web site loads up very fast! What web host are you using?
    Can I get your affiliate link to your host? I wish my website loaded up as
    quickly as yours lol

  15. Hello, i feel that i noticed you visited my web site so i got
    here to go back the choose?.I am trying to find issues to enhance my site!I suppose its good
    enough to make use of some of your ideas!!

  16. Dereketerm表示:

    prednisone online pharmacy: Prednisone Without Prescription – buying prednisone without prescription

  17. StevenNub表示:

    order clomid tablets: generic clomid – order cheap clomid no prescription

  18. Why Pragmatic Experience May Be More Dangerous Than You Believed
    프라그마틱 무료스핀

  19. StevenNub表示:

    buy amoxicillin 250mg: com pharm – amoxicillin 800 mg price

發佈留言

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