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,477,511 Responses

  1. Cảm giác như mình trở thành một phần của cộng đồng khi chơi ở Hitclub! HitClub

  2. I’ve been exploring for a little bit for any high
    quality articles or weblog posts on this sort of house .
    Exploring in Yahoo I eventually stumbled upon this site.
    Reading this information So i’m happy to show that I’ve a very just
    right uncanny feeling I found out just what I
    needed. I most no doubt will make sure to do not fail to remember this site and give it a look regularly.

  3. It’s enormous that you are getting thoughts from
    this article as well as from our dialogue
    made here.

    my page :: realtor in tucson arizona

  4. Josephmax表示:

    https://pinup-kazi.ru/# pinup kazi

  5. DarioTob表示:

    вавада: vavada – вавада казино

  6. Nikefut表示:

    darknet drug links deep dark web dark web sites links

  7. Medicament information sheet. Short-Term Effects.
    cost of generic irbesartan without insurance
    Actual about pills. Read here.

  8. Iyannaobesk表示:

    актуальные фрироллы jack poker
    https://t.me/s/jack_poker_frirollyi

  9. DarioTob表示:

    пин ап казино официальный сайт: pinup-kazi.ru – пин ап казино

  10. Your style is very unique best realtor in saint augustine florida comparison to other folks I have read stuff from.

    I appreciate you for posting when you have the opportunity,
    Guess I’ll just book mark this site.

  11. I loved as much as you’ll receive carried out right here.
    The sketch is attractive, your authored
    subject matter stylish. nonetheless, you command get got an impatience over that you wish
    be delivering the following. unwell unquestionably
    come more formerly again as exactly the same nearly a lot often inside case
    you shield this hike.

    Here is my blog – warm beverage hoodies

  12. DerekWab表示:

    https://www.larson-holz-spb.ru – ваш надежный помощник в выборе и заказе кухонной мебели.

  13. HenrydeT表示:

    pinup пинап казино pinup-kazi.ru

  14. Everything is very open with a clear clarification of the challenges.
    It was really informative. Your website is extremely helpful.
    Thanks for sharing!

  15. Greetings from Ohio! I’m bored to tears at work so I decided to check out your site on my
    iphone during lunch break. I really like the knowledge you present here and can’t wait to take a look when I get home.
    I’m surprised at how quick your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyhow, wonderful site!

  16. Hmm is anyone else having 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.

    Feel free to visit my website :: realtor in volusia county fl

  17. Nikefut表示:

    darknet drug links darknet market links blackweb official website

  18. Jamesdiure表示:

    пин ап кз: пинап казино – pinup-kazi.kz

  19. DarioTob表示:

    пин ап кз: pinup kazi – pinup

  20. Locating the appropriate SR22 insurance coverage is actually important
    to avoid high costs. Try to find discounts and contrast quotes to discover budget friendly
    SR22 insurance options.

  21. Howdy! This article could not be written much better!
    Looking through this post reminds me of my previous
    roommate! He constantly kept talking about this. I am going to forward this article
    to him. Fairly certain he’ll have a great read. Thank you for sharing!

發佈留言

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