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,168,970 Responses

  1. An asbestos lawyer can assist victims file a suit against the business that exposed them to Asbestos Lawsuit.
    A lawsuit can help victims obtain compensation for medical expenses as well as lost wages
    and other damages.

  2. You’ll Be Unable To Guess Online Shopping Sites Top 7’s
    Benefits online shopping sites top 7

  3. 14 Creative Ways To Spend The Leftover Coffee Machine Budget http://www.4182051.xyz

  4. kaymell.uk表示:

    You’ll Never Guess This Casino Slots’s Tricks kaymell.uk

  5. Looking Into The Future What’s The Online Shopping Industry Look Like In 10 Years?
    Best Luxury Online Shopping Sites Uk

  6. arlennizo表示:

    It’s The Ugly Truth About Boot Mobility Scooter arlennizo

  7. swap pdf表示:

    When someone wгites an paragraph hе/she retains the thought of a user in his/her
    brtain thhat how a user can understand it. Tһus that’s whyy
    this paгаgraph is amazing. Thаnks!

    Feel frreе to surf to mуy blog post :: swap pdf

  8. zackfoxworth表示:

    20 Up-Andcomers To Watch The American Fridge Freezer
    Sale Industry zackfoxworth

  9. cs2 cases表示:

    Ten Stereotypes About Why Are CSGO Skins Going Up In Price That Aren’t Always True cs2 cases

  10. Is Your Company Responsible For An Adhd Assessments For Adults Budget?
    12 Best Ways To Spend Your Money adhd assessment for adults what to expect

  11. 20 Fun Facts About Nissan Qashqai Key Fob nissan xterra key fob

  12. Darnell表示:

    Wooden Loft Bed Isn’t As Difficult As You Think bed loft (Darnell)

  13. kubetarchi表示:

    I’ve been exploring for a bit for any high-quality articles or blog posts in this sort of house
    . Exploring in Yahoo I ultimately stumbled upon this web site.
    Studying this info So i am glad to convey that I’ve an incredibly good uncanny feeling I discovered
    exactly what I needed. I so much certainly will make certain to
    don?t disregard this site and give it a glance regularly.

  14. Medicine information sheet. What side effects can this medication cause?
    can i purchase generic ampicillin pill
    Everything trends of pills. Get information here.

  15. dob表示:

    Всем привет! Может кто знает, где найти разные статьи о недвижимости? Сейчас читаю https://an72.ru

  16. Geraldo表示:

    May I just say what a relief to uncover someone who actually knows what they’re discussing on the net.
    You actually realize how to bring a problem to light and make it important.
    A lot more people should look at this and understand this side of the story.
    I was surprised you aren’t more popular because you
    most certainly possess the gift.

  17. You’ll Never Guess This Best Online Shopping Sites Clothes’s Tricks best online shopping sites clothes

  18. Eve表示:

    Thanks on your marvelous posting! I seriously enjoyed reading it,
    you might be a great author.I will make sure
    to bookmark your blog and will come back in the future.
    I want to encourage that you continue your great writing, have a nice morning!

  19. 9 Things Your Parents Taught You About Upvc Window Repairs
    Near Me Window Repairs near me

  20. check it out表示:

    Hey! This is my 1st comment here so I just wanted to give a quick shout out and say I truly enjoy
    reading your articles. Can you recommend any other blogs/websites/forums that deal with the same topics?
    Many thanks!

  21. You made some nice points there. I did a search on the subject and found most guys will agree with your website.

    Also visit my blog post; https://vavadaonlinecasino.evenweb.com/

  22. 15 Things You Didn’t Know About Accident Lawyer Accident attorney

  23. 4182051.xyz表示:

    Could Coffee Pod Machine Be The Key For 2023’s Challenges?
    4182051.xyz

  24. The Most Effective Advice You’ll Ever Receive About Semi Truck Lawsuit semi truck Accident lawyers

發佈留言

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