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,198,213 Responses

  1. The 10 Most Terrifying Things About Upvc
    Door Repairs Near Me upvc Door repairs near Me

  2. site link表示:

    I love it whenever people get together and share ideas.
    Great blog, stick with it!

  3. 7 Small Changes That Will Make The Biggest Difference In Your ADHD
    Diagnosis Adults Private UK Private Assessment For
    Adhd Northern Ireland (http://Aragaon.Net/Bbs/Board.Php?Bo_Table=Review&Wr_Id=412756)

  4. Medication information sheet. Cautions.
    how to buy levonorgestrel without rx
    Everything about pills. Get information here.

  5. Marietta表示:

    See What Psychiatric Assessment Form Tricks The Celebs Are Making
    Use Of psychiatric assessment form (Marietta)

  6. The Next Big Thing In The Double Glazed Units Near Me Industry Replacement Double Glazed Units Near Me

  7. Fantastic stuff, Regards.

  8. Business Card Printing For Graphic Design Firms
    200만원 대출

  9. 10 Facts About Upvc Window Repair Near Me That Can Instantly Put You In A Good Mood upvc window repairs near me

  10. Приветствую. Подскажите, где найти разные статьи о недвижимости? Сейчас читаю https://vortex-los.ru

  11. spousal表示:

    Very nice post. I just stumbled upon your blog and wished
    to say that I have really enjoyed browsing your blog posts.

    In any case I’ll be subscribing on your rss feed and I’m hoping you write again very
    soon!

  12. Jada表示:

    The Secret Life Of Mental Health Assessment initial mental health assessment;
    Jada,

  13. RubenSlild表示:

    Hi there! pharmacy tech online great website.

  14. bokep indo表示:

    Nice post. I was checking constantly this weblog and I’m inspired!
    Extremely helpful info particularly the last section 🙂 I handle such information much.

    I used to be seeking this certain information for a long time.
    Thanks and best of luck.

  15. Kubet表示:

    With thanks! Ample material.

  16. xX1Qjl表示:

    Marvelous, what a webpage it is! This webpage provides helpful information to us, keep
    it up.

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

  18. Отличный сатй! Всем рекомендую!скупка золота москва

  19. Pills prescribing information. Generic Name.
    can i order generic pulmicort for sale
    All what you want to know about medicines. Read information here.

  20. arlennizo表示:

    Three Reasons Why The Reasons For Your Boot Scooters Is Broken (And
    How To Fix It) arlennizo

  21. The Most Hilarious Complaints We’ve Seen About Adhd
    In Women Symptoms adhd Symptoms in Adults uk

  22. dominoqq表示:

    A motivating discussion is definitely worth comment.
    I think that you ought to publish more on this issue, it
    might not be a taboo matter but typically people don’t
    discuss such issues. To the next! Cheers!!

  23. https://atomicketo.net have been gaining popularity in the health and wellness community for their potential benefits in aiding weight loss, improving digestion, and boosting energy levels.

  24. link bokep表示:

    Pretty nice post. I just stumbled upon your weblog and wished to say
    that I have truly enjoyed surfing around your blog posts.

    After all I’ll be subscribing to your feed and
    I hope you write again soon!

  25. http://www.daecheonkim.kr/bbs/board.php?bo_table=free&wr_id=163658

    I think this is among the most vital information for me.
    And i am glad reading your article. But should remark on few general things, The
    wensite style is ideal, the articles is really
    nice : D. Good job, cheers

  26. Medicine information for patients. Drug Class.
    where to get cheap minocycline
    Best what you want to know about drug. Read now.

  27. Tuyet表示:

    Neat blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple adjustements would
    really make my blog jump out. Please let me know where you got your
    design. Many thanks

  28. How To Recognize The Upvc Repairs Near Me That’s Right For You
    Window repair Near me

  29. Five Lessons You Can Learn From Window Repair Near Upvc Window Repair Near Me

  30. Medicines information for patients. Drug Class.
    can i buy generic bactrim
    Everything information about medicines. Get information here.

發佈留言

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