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,147,447 Responses

  1. Lynda表示:

    Quiz: How Much Do You Know About Online Shopping Sites Top 7?
    Pocket Watch With Chain, Lynda,

  2. Wonderful data, Many thanks!

  3. This Is A Guide To Car Accident Claim In 2023 car accident law firm

  4. 10 Sites To Help You Develop Your Knowledge About How Many Cases Are There In CSGO Cs2
    cases (cf58051.tmweb.Ru)

  5. Desfrute da emocao no melhor cassino Blaze

  6. KennethFom表示:

    מהעבודה! פינוק מלא תשוקה ללא מחויבות אם אתם רוצים לבלות זמן עם נערת עיסויים מלאת תשוקה שמחכה לכם במקומות כמו דירות דיסקרטיות נפגשות עם מקומיים וגם עם אורחים. באר שבע מזמינה גברים באופן קבוע להגיע לעסקים וגם כתיירים. נערות ליווי בדרום הסקסיות עוזרות לכל Tel Aviv escort girls for pleasing the pickiest men

  7. Velda表示:

    20 Best Tweets Of All Time About Reprogramming Car Key reprogramme car key (Velda)

  8. Gpsites.Win表示:

    Interactive Slots Tools To Ease Your Everyday Lifethe Only Interactive Slots
    Trick That Everybody Should Learn Interactive
    Slots – Gpsites.Win,

  9. Online Shopping Uk: It’s Not As Expensive As You
    Think Multi-Function Cable Set

  10. Door Fitters Crawley It’s Not As Hard As You Think Window doctor near me

  11. I’m amazed, I have to admit. Seldom do I come across a blog that’s equally educative and
    engaging, and let me tell you, you’ve hit the nail on the head.
    The problem is something not enough people are speaking intelligently about.
    I am very happy that I stumbled across this during my hunt for something relating to this.

    My site; java burn reviews

  12. I was recommended this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my difficulty.
    You’re amazing! Thanks!

  13. Thanks for any other wonderful article. The place
    else could anyone get that type of information in such a perfect way of writing?
    I’ve a presentation next week, and I am at the search for
    such info.

  14. memek basah表示:

    Link exchange is nothing else except it is just placing the other person’s web site link on your page at suitable
    place and other person will also do similar for
    you.

  15. Hello to every one, the contents present at this web site are genuinely remarkable for people knowledge, well, keep up the good work
    fellows.

  16. 20 Things You Need To Be Educated About Slot Gaming Experience Top casino slots (https://Images.google.Cf/)

  17. What To Say About Online Clothes Shopping Near Me To
    Your Mom Anatomy Learning Kit For Kids

  18. 12 Companies Leading The Way In Upvc Window Repair Near
    Me upvc window repairs Near me [http://highnfirst.Site]

  19. gre01.com表示:

    The 10 Most Scariest Things About Double Glazing Near Me double
    glazing near Me (gre01.com)

  20. Who Is Trusted Online Shopping Sites For Clothes And Why You Should Be Concerned Custom size frame arttoframes

  21. 17 Signs You Are Working With Cheapest Online Grocery Shopping Uk Little Big Shot Plunger

  22. A Look At The Ugly Truth About Window Handles Replacement
    Window Cost Replacement

  23. How To Solve Issues Related To Car Accident Claim Car Accident Lawsuits

  24. Caitlin表示:

    Immigration issues can usually be sophisticated and obscure.
    When you’ve got any problems at all with immigration rules or processes,
    an immigration lawyer could be of much assist.

    Here is my site :: UK Immigration Attorney (Caitlin)

  25. Diplom_wdKl表示:

    купить диплом провизора купить диплом провизора .

  26. Diplom_avKl表示:

    купить диплом http://ukr-diplom.ru .

  27. 5 Tools Everyone Is In The 3d Slots Industry Should Be Utilizing slot Machine reviews (http://Voprosi-otveti.ru/user/pastacry77)

  28. jihad表示:

    I visited multiple web pages but the audio feature for audio songs existing at this web page
    is truly marvelous.

  29. 30 Inspirational Quotes For Semi Truck Litigation Semi truck accident lawyers

  30. What’s The Current Job Market For Double Glazed Window Repairs Professionals?

    Double Glazed Window Repairs (https://Wolvesbaneuo.Com/Wiki/Index.Php/User:GordonBisbee04)

發佈留言

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