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,211,686 Responses

  1. 3 Ways That The Porsche Car Key Will Influence Your Life Porsche cayenne key battery

  2. A Proactive Rant About Workers Compensation Legal Workers’ compensation Lawsuit

  3. WayneCoirl表示:

    reputable mexican pharmacies online: mexican drugstore online – buying prescription drugs in mexico online

  4. Arnoldten表示:

    medication from mexico pharmacy pharmacies in mexico that ship to usa mexico drug stores pharmacies

  5. DominicJaw表示:

    medicine in mexico pharmacies: mexican drugstore online – п»їbest mexican online pharmacies

  6. Nelsonbluck表示:

    mexican border pharmacies shipping to usa: buying prescription drugs in mexico – mexican drugstore online

  7. Dwight表示:

    Best Budget Coffee Machine Techniques To Simplify Your Daily
    Lifethe One Best Budget Coffee Machine Trick Every Individual Should Be Able To best
    budget coffee machine [Dwight]

  8. treadmills uk表示:

    Five Killer Quora Answers To Treadmills UK treadmills uk

  9. Arnoldten表示:

    buying from online mexican pharmacy buying prescription drugs in mexico online mexican pharmacy

  10. WayneCoirl表示:

    mexico drug stores pharmacies: reputable mexican pharmacies online – п»їbest mexican online pharmacies

  11. I have read several just right stuff here. Definitely
    price bookmarking for revisiting. I wonder how a lot effort you put to
    make this kind of wonderful informative website.

    My web-site denticore reviews

  12. WayneCoirl表示:

    mexican online pharmacies prescription drugs: reputable mexican pharmacies online – mexican drugstore online

  13. Don’t Buy Into These “Trends” About Montpellier Retro Fridge Freezer Side By Side Fridge Freezer With Temperature Control

  14. What’s The Current Job Market For Bmw Series 1 Key Professionals?
    Bmw Series 1 Key

  15. Arnoldten表示:

    buying prescription drugs in mexico online mexican rx online buying prescription drugs in mexico

  16. WayneCoirl表示:

    mexican online pharmacies prescription drugs: mexican rx online – medicine in mexico pharmacies

  17. parterapeut表示:

    I have been browsing online more than 2 hours today, yet I
    never found any interesting article like yours.
    It’s pretty worth enough for me. In my view, if all web owners and bloggers made good content as you
    did, the web will be a lot more useful than ever before.

  18. DominicJaw表示:

    mexico drug stores pharmacies: mexico drug stores pharmacies – mexican drugstore online

  19. WayneCoirl表示:

    mexico drug stores pharmacies: buying prescription drugs in mexico – reputable mexican pharmacies online

  20. https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/ya-chinavisa-1/validity-period-and-renewal-of-taiwan-compatriot-permit-3.html
    My daughter desires of a wedding on a seaside in Bali, so the place will that leave me I wonder.

  21. https://yacss2024.s3.us-east-005.backblazeb2.com/ya-drbogin-27/the-connection-between-massage-and-detoxification.html
    We requested some wedding fashion specialists to determine what a MOB should wear on the massive day.

  22. https://intant.kz is a dietary supplement that claims to support weight loss by promoting ketosis and using apple cider vinegar (ACV) as one of its key ingredients.

  23. WayneCoirl表示:

    medication from mexico pharmacy: mexico pharmacies prescription drugs – mexico drug stores pharmacies

  24. DominicJaw表示:

    mexican border pharmacies shipping to usa: mexico pharmacies prescription drugs – buying from online mexican pharmacy

  25. The 10 Scariest Things About Folding Electric Wheelchairs
    Uk folding electric wheelchairs uk (mobility-scooters-Cheap28483.designertoblog.com)

  26. Nelsonbluck表示:

    mexican pharmaceuticals online: mexican drugstore online – mexico pharmacies prescription drugs

  27. https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/ya-drbogin-10/how-massage-therapy-can-aid-in-recovery-from-injury.html
    The website’s sophisticated robes make for wonderful night wear that’ll serve you long after the wedding day.

  28. 人形 えろ表示:

    Your ability to weave complex theories with practical applications is truly commendable,ラブドールmaking the article not only informative but also highly actionable.

  29. Nelsonbluck表示:

    п»їbest mexican online pharmacies: mexican drugstore online – purple pharmacy mexico price list

發佈留言

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