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,107 Responses

  1. macauslot88表示:

    I don’t even understand how I stopped up here, however I thought this post was once good.
    I do not realize who you’re but certainly you’re going to
    a well-known blogger for those who are not already.

    Cheers!

  2. AlbertDuM表示:

    cytotec buy online usa: cyt premium – cytotec pills buy online

  3. Excellent post. I am facing some of these issues as well..

  4. BernardMop表示:

    minocycline 50mg without doctor buy Ivermectin online buy oral ivermectin

  5. Rhoda Jackson表示:

    Robótki na drutach to sztuka szydełkowe dzieła

  6. вывод из запоя в стационаре воронежа https://zarabotokdoma.creartuforo.com/viewtopic.php?id=11483/ .

  7. I was recommended this web site by my cousin. I am not sure whether this post
    is written by him as no one else know such detailed about my
    problem. You are incredible! Thanks!

  8. DarrenUnjuh表示:

    https://lisinopril1st.com/# cheapest Lisinopril

  9. DarrenUnjuh表示:

    https://iverfast.com/# minocycline for sinus infection

  10. 10 Meetups About Mens Masturbator You Should Attend Mens Masterbator

  11. BrandenSlomy表示:

    order cheap clomid without prescription: buy clomid – how can i get cheap clomid

  12. DarrenUnjuh表示:

    http://iverfast.com/# buy stromectol online uk

  13. What’s up friends, how is everything, and what you would like to say concerning this post, in my view its actually amazing for me.

  14. ស្វែងរកកាស៊ីណូអនឡាញដ៏ល្អបំផុតនៅក្នុងប្រទេសកម្ពុជានៅ GOD55
    សម្រាប់បទពិសោធន៍លេងហ្គេមដ៏គួរឱ្យទុកចិត្ត
    និងរំភើបជាមួយនឹងការឈ្នះដ៏ធំ។

  15. BrandenSlomy表示:

    amoxicillin 500mg capsules uk: buy amoxil online – buy cheap amoxicillin online

  16. What’s The Most Important “Myths” Concerning Double Dildo Use Could Be True double ended dildoes

  17. BernardMop表示:

    buy Lisinopril 1st buy Lisinopril online lisinopril 5 mg tablet cost

  18. DarrenUnjuh表示:

    http://lisinopril1st.com/# lisinopril buy without prescription

  19. Nikefut表示:

    darkmarkets dark web websites darknet links

  20. выведение из запоя воронеж стационар https://gaslo.ukrbb.net/viewtopic.php?f=13&t=3413/ .

  21. DarrenUnjuh表示:

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

發佈留言

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