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,478,769 Responses

  1. I’m really impressed with your writing skills and also with the layout on your blog.
    Is this a paid theme or did you modify it yourself?
    Anyway keep up the nice quality writing, it is rare to
    see a great blog like this one these days.

  2. DarrenUnjuh表示:

    http://lisinopril1st.com/# lisinopril1st

  3. DarrenUnjuh表示:

    http://iverfast.com/# minocycline 50 mg para que sirve

  4. BernardMop表示:

    Abortion pills online cyt premium buy cytotec over the counter

  5. DarrenUnjuh表示:

    http://cytpremium.com/# buy cytotec online

  6. A Peek Inside The Secrets Of Fleshlight Fleshlight Brand

  7. Peculiar article, exactly what I was looking for.

  8. zestril表示:

    The tests also revealed that most people https://safestcasinosites.co.uk/.

  9. Good day I am so delighted I found your website, I really
    found you by mistake, while I was searching on Google for something else, Anyhow
    I am here now and would just like to say cheers for
    a tremendous post and a all round entertaining blog (I
    also love the theme/design), I don’t have time to look over it all at
    the minute but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read a lot more,
    Please do keep up the superb jo.

  10. Nikefut表示:

    darkmarket dark market onion dark market

  11. BrandenSlomy表示:

    amoxicillin 500mg capsules antibiotic: amoxicillin order online no prescription – amoxicillin 500mg capsules uk

  12. 출장안마表示:

    Hey very interesting blog!

  13. xnxx.com表示:

    hello!,I love your writing very so much! share we be in contact more about your article on AOL?

    I need an expert on this area to resolve my problem.
    May be that is you! Having a look forward to peer
    you.

  14. acls training表示:

    I blog quite often and I really thank you for your information. Your article has
    really peaked my interest. I am going to bookmark
    your website and keep checking for new details about once per week.
    I subscribed to your Feed too.

  15. Davao表示:

    Poker For Beginners Davao

  16. Most activity trackers start out with a fixed goal – usually around
    10,000 steps – or let you set your individual manually,
    and there it ends.

    Also visit my webpage สล็อตออนไลน์

  17. Wow! At last I got a blog from where I know how to genuinely
    get valuable facts regarding my study and knowledge.

發佈留言

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