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,629,633 Responses

  1. Do you have a spam issue on this website; I also am a blogger, and I was wondering
    your situation; we have developed some nice methods and we are looking to
    trade methods with other folks, why not shoot me an email if interested.

  2. 7 Easy Tips For Totally Making A Statement With Your Train Injury Lawsuit trucking accident injury lawyer

  3. Danieljek表示:

    order amoxicillin 500mg http://clomidrexpharm.com/# where to buy clomid without prescription

  4. Everett表示:

    Feel free to visit my site Daycares By Category
    (Everett)

  5. Casino表示:

    No matter if some one searches for his essential thing, therefore he/she wants to be available
    that in detail, therefore that thing is maintained over here.

  6. Dereketerm表示:

    Priligy tablets: buy dapoxetine online – priligy maxpharm

  7. StevenNub表示:

    amoxicillin for sale: Amoxicillin for sale – where to buy amoxicillin pharmacy

  8. Appreciation to my father who told me concerning this website, this website is actually awesome.

  9. Magnificent goods from you, man. I’ve understand your stuff previous to and you are just too magnificent.
    I really like what you’ve acquired here, really like what you are saying and the way in which
    you say it. You make it enjoyable and you still care
    for to keep it smart. I cant wait to read much more from you.

    This is actually a terrific site.

  10. Danieljek表示:

    how much is amoxicillin prescription http://amoxilcompharm.com/# amoxicillin 500mg no prescription

  11. StevenNub表示:

    can i order clomid pills: generic clomid – can i purchase generic clomid without rx

  12. Hi there, i read your blog from time to time and i own a similar one and i was
    just curious if you get a lot of spam feedback?
    If so how do you stop it, any plugin or anything you can suggest?
    I get so much lately it’s driving me mad so any assistance is
    very much appreciated.

  13. VrnHesia表示:

    नया ऑनलाइन कैसीनो असली नकद गेम ऑनलाइन दमन कैसीनो

  14. Either way, transferring files between two smartphones is quick and simple.

  15. BrandenSlomy表示:

    prednisone in canada: prednisone in mexico – cortisol prednisone

  16. If you want to obtain much from this post then you have to apply such strategies to your won website.

  17. Danieljek表示:

    amoxicillin order online https://priligymaxpharm.com/# priligy maxpharm

  18. Dereketerm表示:

    where can i buy cheap clomid online: clomid rex pharm – where buy generic clomid without rx

  19. Bradleygoath表示:

    cortisol prednisone buy prednisone prednisone 20mg prescription cost

發佈回覆給「Cazrahx」的留言 取消回覆

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