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,245,719 Responses

  1. Thanks for sharing your info. I really appreciate
    your efforts and I am waiting for your further post thank you once again.

  2. Markdob表示:

    Всем привет! Подскажите, где найтиполезные статьи о недвижимости? Сейчас читаю – https://cspstroy.ru

  3. Тут можно преобрести ящик для оружия купить магазин сейфы для оружия

  4. StevenNub表示:

    prednisone 7.5 mg: Prednisone Without Prescription – prednisone buy canada

  5. BrandenSlomy表示:

    can you get clomid price: clomid purchase online rex pharm – where to buy generic clomid pills

  6. StevenNub表示:

    amoxicillin generic: amoxil com pharm – amoxicillin 500mg capsules price

  7. StevenNub表示:

    priligy max pharm: buy dapoxetine online – priligy maxpharm

  8. Hi, i think that i saw you visited my site thus i came to “return the favor”.I’m trying
    to find things to improve my website!I suppose its ok to use a few of your ideas!!

  9. Markdob表示:

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

  10. DrableHub表示:

    I do agree with all the ideas you’ve presented to your post.
    They are really convincing and can certainly work.
    Nonetheless, the posts are too brief for beginners.
    May just you please prolong them a little from next time?
    Thank you for the post.

  11. Nikefut表示:

    dark market link dark markets 2024 deep web search

  12. Dereketerm表示:

    priligy maxpharm: priligy – max pharm

  13. Markdob表示:

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

  14. sosro togel表示:

    Fantastic article Ill definitely be coming back to this for reference
    sosro togel

  15. Dereketerm表示:

    order amoxicillin online: cheap amoxil – amoxicillin tablet 500mg

  16. In recent years, http://w.dainelee.net/cgi-bin/pldbbs/pldbbs.cgi?p=1&ar=000434&comment=477&count=1&ie=1%5Dbuy products have gained immense popularity in the health and wellness industry. As more people seek natural alternatives to traditional medicine, CBD has emerged as a powerful plant-based remedy for various ailments.

  17. BrandenSlomy表示:

    priligy: max pharm – priligy max pharm

  18. Drug information sheet. Cautions.
    metformin hospitalized patients
    Best trends of meds. Get information now.

  19. 티비위키表示:

    I really appreciate your effort in helping people get the information they need 티비위키

  20. Dereketerm表示:

    buy cheap amoxicillin online: cheap amoxil – can i buy amoxicillin online

  21. Nikefut表示:

    darkmarkets best darknet markets darknet market list

  22. BrandenSlomy表示:

    dapoxetine price: max pharm – max pharm

發佈留言

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