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,466,940 Responses

  1. Frances Ball表示:

    If you prefer a customized adventure in Washington DC, think about making a reservation for an exclusive excursion private limo service

  2. Gennie表示:

    Hey there! Someone in my Myspace group shared this site with us so I came to check it out.
    I’m definitely loving the information. I’m
    book-marking and will be tweeting this to my followers!

    Fantastic blog and fantastic design and style.

  3. 9 . What Your Parents Taught You About American Fridge
    Freezer Sale american Fridge freezer sale

  4. 인천유흥表示:

    Adult Entertainment 인천유흥

  5. 11 Strategies To Completely Redesign Your Asbestosis Asbestos Mesothelioma Attorney mesothelioma lawsuits (https://lovewiki.faith/wiki/pottshaastrup1490)

  6. Andre Robbins表示:

    I have actually been to numerous Brazilian steakhouses, and every one has its very own unique twist on typical dishes brazilian buffet steakhouse

  7. Cody Perkins表示:

    This was quite helpful. For more, visit cake shop near me

  8. Thanks for the informative post. More at roofing service orlando fl

  9. Salvatore表示:

    Massage Therapy: What Just A Few Ingredients To Learn It 유흥 (Salvatore)

  10. нарколог на дом вывод из запоя ростов нарколог на дом вывод из запоя ростов .

  11. наркология вывод из запоя ростов наркология вывод из запоя ростов .

  12. Used appropriately, a land study will certainly make your development go
    as efficiently as possible.

  13. Alisha表示:

    A person essentially assist to make significantly posts I’d state.
    That is the first time I frequented your web page and thus far?
    I amazed with the analysis you made to create this actual put up extraordinary.
    Magnificent activity!

  14. BradleyEdino表示:

    deneme bonusu veren siteler yeni denemebonusuverensiteler25 yeni deneme bonusu veren siteler

  15. FrankEmine表示:

    en guvenilir casino siteleri: gГјvenilir deneme bonusu veren siteler – deneme bonusu veren casino siteleri

  16. stamps maker [url=https://www.stamp-creator-online0.com]stamps maker[/url] .

  17. An impressive share! I have just forwarded this onto a
    friend who has been conducting a little homework
    on this. And he actually bought me breakfast simply because I discovered it for him…
    lol. So let me reword this…. Thanks for the meal!!
    But yeah, thanks for spending some time to discuss this subject here on your internet
    site.

  18. If you’re in Houston and need to ship a vehicle Houston vehicle transport

  19. I’m no longer positive where you are getting your info, however good topic.
    I needs to spend some time learning much more or figuring out more.
    Thank you for excellent info I was in search of this info for my mission.

  20. BradleyEdino表示:

    yeni deneme bonusu veren siteler deneme bonusu veren yeni siteler deneme bonusu veren siteler yeni

  21. JasonUnsoG表示:

    https://denemebonusuverensiteler25.com/# yeni deneme bonusu veren siteler

  22. Alan Fisher表示:

    I enjoyed this article. Check out Roof Repair Mesa for more

  23. Cyprus is lucky to have such talented contractors like Pool remodelers near me

  24. I am regular reader, how are you everybody? This paragraph posted
    at this website is truly fastidious.

發佈留言

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