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,156,700 Responses

  1. 5 Killer Quora Answers On Online Shopping Uk
    Women’s Clothing online shopping uk women’s clothing

  2. I quite like reading a post that will make men and women think.
    Also, thank you for allowing for me to comment!

  3. tonic greens表示:

    Highly energetic post, I loved that a lot. Will
    there be a part 2?

    Look into my blog: tonic greens

  4. Good post. I definitely love this site. Thanks!

  5. Dian表示:

    The drink was initially bottled in inexperienced
    glass bottles with crown caps. Throughout this process,
    the drink loses a few of its sweetness and eventually resembles sparkling wine.

    Elderberry sparkling wine is made from the
    flowers of the black elderberry, also known as lilac in northern Germany, water, sugar, lemon slices and acid (tartaric or citric acid) or vinegar and adjustments style
    and character during pure fermentation. On the following pages,
    sixteen plastic and metallic drinking bottles have been tested.
    The sizes of the drinking bottles are based mostly on the amount of protein shakes you devour.
    Suitable for bottles of all sizes akin to: B.
    200 ml, 250 ml, 330 ml, 500 ml, 750 ml or one thousand ml,
    we provide a big number of colors in our in depth range
    and naturally we not solely print glass water bottles along with your individual emblem.
    It will likely be significant to match how typically such
    a bottle may be reused, while those made of plastic, for instance, should be disposed
    of after every use.

  6. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://btelectricshop.es/dgmnh of type and scrambled it to make a type specimen book.

  7. Guide To Slot Themes: The Intermediate Guide For Slot Themes Slot themes; http://inprokorea.com,

  8. 10 Inspirational Graphics About Semi Truck Legal
    Semi Truck Accident Lawsuits

  9. For those who find social situations challenging, have irontech dollgone through difficult relationship experiences, or feel out of place in the regular dating scene

  10. BLOG表示:

    This is my first time visit at here and i am truly happy to
    read everthing at single place.

  11. fun8825239表示:

    Everyone loves what you guys tend to be up too. This sort of clever work and coverage!

    Keep up the amazing works guys I’ve included you guys to my personal blogroll.

  12. I believe that is among the such a lot significant information for
    me. And i’m glad studying your article. But should observation on few general issues, The web site taste is wonderful, the
    articles is actually nice : D. Just right task, cheers

  13. Klara表示:

    Learn What Veterans Disability Lawsuit Tricks The Celebs Are Utilizing
    veterans disability law firm, Klara,

  14. 10 Easy Steps To Start Your Own Accident Case Business accident lawsuit

  15. 15 Shocking Facts About Best Online Shopping Sites
    For Clothes That You Didn’t Know best Luxury online shopping sites uk

  16. TAMPUNG99表示:

    It’s perfect time to make a few plans for the
    longer term and it’s time to be happy. I have learn this
    put up and if I could I want to counsel you few fascinating issues or advice.
    Perhaps you could write next articles relating to this article.
    I want to learn even more things approximately it!

  17. You’ll Never Guess This Double Glaze Repair Near Me’s Tricks
    double glaze repair near me

  18. Guide To Double Glazed Window Near Me: The Intermediate Guide In Double Glazed Window Near Me double glazed window Near me

  19. What’s The Job Market For Uk Women’s Online Shopping Websites Professionals?

    uk women’s online shopping websites

  20. 10 Things You Learned In Preschool To Help You Get A
    Handle On Car Accident Litigation Car Accident Attorney

  21. Darwin表示:

    20 Quotes That Will Help You Understand Veleco Mobility veleco
    cristal mobility scooter (Darwin)

  22. baskili poset表示:

    magnificent points altogether, you simply gained a logo new reader.
    What would you suggest in regards to your submit that you just made some days
    in the past? Any sure?

  23. Levi表示:

    10 Locations Where You Can Find Real Money Slots video slots – Levi

  24. I am sure this piece of writing has touched all the internet people, its really really
    pleasant article on building up new website.

  25. A Step-By-Step Guide To Slot Developers From Beginning To End Real Casino Slots

發佈留言

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