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,248,994 Responses

  1. Rodrick表示:

    What’s The Job Market For Can Mobility Scooters Go On Road Professionals?
    can mobility scooters go on road (Rodrick)

  2. porn korea表示:

    I’m really inspired together with your writing talents as neatly as with the structure in your
    blog. Is this a paid theme or did you customize it yourself?
    Either way stay up the excellent quality writing, it’s uncommon to see a great
    blog like this one these days..

  3. Curtisduh表示:

    BBgate MarketPlace 2024 Breaking Bad Gate Forum
    BBgate MarketPlace

  4. I every time spent my half an hour to read this webpage’s content daily along with
    a cup of coffee.

  5. Dereketerm表示:

    order generic clomid online: clomid – where to get cheap clomid without dr prescription

  6. What’s up, just wanted to say, I loved this post.

    It was inspiring. Keep on posting!

  7. porn表示:

    It’s very straightforward to find out any topic on net as compared to textbooks, as I found this
    article at this web page.

  8. BrandenSlomy表示:

    max pharm: priligy maxpharm – cheap priligy

  9. Medication prescribing information. Short-Term Effects.
    where can i buy cheap azathioprine without a prescription
    All news about meds. Get information now.

  10. BrandenSlomy表示:

    medicine amoxicillin 500mg: Amoxicillin Com Pharm – amoxicillin 500 mg where to buy

  11. How To Know The Convertible Sleeper Sofa That’s Right
    For You Comfortable Sleep Sofas

  12. It’s The Perfect Time To Broaden Your Renault Megane Key Card Replacement Options renault kadjar keyless entry

  13. Dereketerm表示:

    how to buy amoxycillin: amoxil com pharm – amoxicillin capsules 250mg

  14. Dereketerm表示:

    cheap priligy: dapoxetine online – buy priligy

  15. Markdob表示:

    Всем привет! Подскажите, где найтиполезные блоги о недвижимости? Пока нашел – https://finkom74.ru

  16. JacobLoody表示:

    фабрика кухня — Прямые поставки от производителя: кухни высокого качества.

  17. My family members all the time say that I am wasting my
    time here at net, however I know I am getting experience
    daily by reading such nice articles.

  18. top prams表示:

    Guide To Top Prams: The Intermediate Guide In Top Prams top prams

  19. It’s really a nice and helpful piece of info. I’m glad that you
    shared this useful information with us. Please stay us up to date like this.
    Thanks for sharing.

  20. Markdob表示:

    Приветствую. Подскажите, где почитатьполезные статьи о недвижимости? Пока нашел – https://finkom74.ru

  21. StevenNub表示:

    rexall pharmacy amoxicillin 500mg: Amoxicillin buy online – 875 mg amoxicillin cost

  22. Markdob表示:

    Приветствую. Может кто знает, где почитатьполезные статьи о недвижимости? Пока нашел – https://finkom74.ru

  23. StevenNub表示:

    prednisone 20mg online pharmacy: prednisone ray pharm – where can i get prednisone

  24. StevenNub表示:

    buy amoxicillin online mexico: Amoxicillin for sale – amoxicillin 500mg for sale uk

  25. Curtisduh表示:

    BBgate MarketPlace 2024 Breaking Bad Gate Forum
    BBgate MarketPlace

  26. Wet Basement表示:

    If some one needs expert view concerning blogging and site-building then i
    recommend him/her to pay a quick visit this weblog, Keep up the fastidious job.

  27. Dereketerm表示:

    order amoxicillin no prescription: Amoxicillin Com Pharm – amoxicillin discount coupon

  28. Markdob表示:

    Приветствую. Может кто знает, где найтиполезные блоги о недвижимости? Сейчас читаю – https://finkom74.ru

發佈留言

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