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,228,365 Responses

  1. Wonderful beat ! I would like to apprentice while you
    amend your website, how could i subscribe for a blog website?
    The account helped Physiotherapie Near Me a acceptable deal.
    I had been a little bit acquainted of this your broadcast
    offered bright clear idea

  2. Replacement Car Keys Tools To Help You Manage Your
    Everyday Lifethe Only Replacement Car Keys Technique Every Person Needs To Learn Replacement car Keys

  3. WilliamRhype表示:

    http://nolvadex.life/# nolvadex gynecomastia

  4. Your style is very unique in comparison to other people I’ve read stuff from.

    Thank you for posting when you’ve got the opportunity, Guess I will just book mark this web site.

  5. 20 Double Glazing Offers Near Me Websites Taking The Internet By Storm Double
    Glased Windows (Oneal-Vasquez.Mdwrite.Net)

  6. Robinguery表示:

    Сдача автомобиля на разборку – это процесс, который может потребоваться в случае серьезных поломок или старения транспортного средства https://start.me/w/arN1LE

  7. What Is Mesothelioma Law And Why Is Everyone Talking About It?

    Asbestos Lawsuit

  8. WilliamRhype表示:

    http://cytotec.club/# buy cytotec

  9. How To Choose The Right Window Repairs On The Internet double glazing
    near me (https://Wikidot.win/)

  10. Alba表示:

    See What Gotogel Link Alternatif Tricks The Celebs Are Using gotogel link
    alternatif (Alba)

  11. Monty表示:

    Responsible For An Upvc Window Repairs Budget? 10 Amazing Ways To Spend Your Money upvc window repairs near me (Monty)

  12. A Step-By-Step Guide To Magimix Nespresso Machine Nespresso Coffee Machines uk

  13. Mesothelioma Compensation: The Good, The Bad, And The Ugly Mesothelioma case

  14. Guide To Private ADHD Assessment Online: The Intermediate Guide For Private ADHD
    Assessment Online private adhd assessment online (images.google.com.gt)

  15. 10 Windows Repair Near Me-Friendly Habits To Be Healthy
    upvc door repairs Near Me

  16. How To Find Out If You’re Ready To Private Psychiatrist Nottingham Psychiatrist manchester Private

  17. See What Double Glazing Repair Near Me Tricks The Celebs Are Using Double Glazing Repair Near Me
    (http://Web011.Dmonster.Kr)

  18. taka77表示:

    Link exchange is nothing else except it is simply placing the other person’s blog link on your page at appropriate
    place and other person will also do same in favor of
    you.

  19. Packs表示:

    The Reasons You Shouldn’t Think About Enhancing Your Welcome Kit Packs

  20. Wow that was strange. I just wrote an incredibly long comment
    but after I clicked submit my comment didn’t appear.
    Grrrr… well I’m not writing all that over again.
    Anyhow, just wanted to say wonderful blog!

  21. Are You Responsible For The Mesothelioma Compensation Budget?
    Twelve Top Ways To Spend Your Money Mesothelioma Settlement

  22. porn hub表示:

    Ahaa, its good discussion about this post here at this weblog,
    I have read all that, so at this time me also commenting here.

  23. price表示:

    11 “Faux Pas” You’re Actually Able To Create Using Your Genuine Bmw Replacement Key price

  24. click here表示:

    I think this is among the most significant information for me.
    And i’m satisfied reading your article. But should observation on some common issues, The website taste is perfect, the articles is in reality excellent : D.
    Just right process, cheers

  25. Davidtes表示:

    buying cheap propecia no prescription order cheap propecia without a prescription get cheap propecia without insurance

  26. WilliamRhype表示:

    http://nolvadex.life/# nolvadex online

  27. WilliamRhype表示:

    http://ciprofloxacin.tech/# п»їcipro generic

  28. training表示:

    Treadmills 101 A Complete Guide For Beginners training

發佈留言

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