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

  1. DominicJaw表示:

    mexico drug stores pharmacies: mexican mail order pharmacies – buying from online mexican pharmacy

  2. 台北 推拿表示:

    https://bogin17.research.au-syd1.upcloudobjects.com/research/bogin17-(122).html
    If full skirts and punchy prints aren’t your mom’s go-to, try a press release sleeve.

  3. https://accounting035.netlify.app/research/accounting035-(289)
    This retro and stylish cocktail costume contains a full-lace overlay excellent for a night marriage ceremony.

  4. Mohamed表示:

    There’s Enough! 15 Things About Replace Upvc Window Handle We’re
    Tired Of Hearing upvc windows repair near me (Mohamed)

  5. WayneCoirl表示:

    medication from mexico pharmacy: pharmacies in mexico that ship to usa – reputable mexican pharmacies online

  6. The Worst Advice We’ve Heard About Cerebral Palsy
    Claim cerebral palsy Law Firms

  7. Nelsonbluck表示:

    mexico drug stores pharmacies: mexico drug stores pharmacies – buying prescription drugs in mexico

  8. 宜蘭外燴表示:

    https://storage.googleapis.com/catering19/research/catering19-(169).html
    Another dress with ruching for you as I think ruched kinds are so flattering.

  9. Pills information for patients. Short-Term Effects.
    where buy generic chlorpromazine without insurance
    All what you want to know about meds. Get here.

  10. 台中整脊表示:

    https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/drbo3/research/drbo3-(447).html
    Make it pop with a blinged-out pair of heels and matching accessories.

  11. WayneCoirl表示:

    purple pharmacy mexico price list: mexican border pharmacies shipping to usa – mexico drug stores pharmacies

  12. Nelsonbluck表示:

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

  13. 25 Surprising Facts About Dangerous Drugs
    Lawsuit dangerous drugs lawyers

  14. Arnoldten表示:

    mexico drug stores pharmacies best online pharmacies in mexico mexican drugstore online

  15. Lexus Keyfob表示:

    The Most Hilarious Complaints We’ve Seen About Lexus Ct200h Key Replacement Cost
    Lexus Keyfob

  16. It’s remarkable designed for me to have a site, which is
    beneficial in favor of my know-how. thanks admin

  17. چت سکسی表示:

    You actually reported this effectively.

  18. DominicJaw表示:

    medication from mexico pharmacy: mexico drug stores pharmacies – mexican drugstore online

  19. Thank you, I’ve recently been searching for information approximately this topic for
    ages and yours is the greatest I’ve found out till now. However,
    what in regards to the bottom line? Are you certain about
    the source?

    Here is my web-site :: denticore where to buy

  20. What Is What Are Anxiety Symptoms And Why Is Everyone Talking About It?
    generalized anxiety disorder physical Symptoms [sutherland-mccarthy-3.blogbright.Net]

  21. Lost car key表示:

    A Guide To Suzuki Car Key Replacement Cost Uk From Beginning To End Lost car key

  22. evisa vietnam表示:

    It’s the best time to make a few plans for
    the future and it is time to be happy. I have
    read this post and if I could I want to recommend you some attention-grabbing
    issues or suggestions. Perhaps you could write next articles relating to
    this article. I wish to read more issues about it!

  23. Shop Online Uk Tools To Make Your Daily Life Shop Online Uk Trick
    That Everybody Should Learn shop Online Uk

  24. Nelsonbluck表示:

    mexican mail order pharmacies: pharmacies in mexico that ship to usa – mexican rx online

  25. Arnoldten表示:

    mexican pharmacy mexican drugstore online buying from online mexican pharmacy

  26. Nelsonbluck表示:

    mexico drug stores pharmacies: mexican border pharmacies shipping to usa – pharmacies in mexico that ship to usa

  27. DominicJaw表示:

    purple pharmacy mexico price list: buying prescription drugs in mexico online – medicine in mexico pharmacies

  28. WayneCoirl表示:

    mexico drug stores pharmacies: medicine in mexico pharmacies – best online pharmacies in mexico

  29. How Window Seal Replacement Became The Hottest Trend Of 2023 glass replacement for windows near me

發佈留言

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