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,179,011 Responses

  1. bokep indo表示:

    wonderful issues altogether, you just won a emblem new reader.
    What could you recommend about your post that you just made some days in the past?
    Any sure?

  2. toptan poset表示:

    Wow, wonderful blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site is magnificent, let alone the
    content!

  3. Ernestine表示:

    20 Trailblazers Setting The Standard In Double Glazing Repair Near Me double glazing near me (Ernestine)

  4. I have realized some new points from your web site about computer systems. Another thing I’ve always considered is that computers have become a product that each family must have for many reasons. They provide convenient ways in which to organize homes, pay bills, shop, study, listen to music and perhaps watch tv programs. An innovative technique to complete most of these tasks is with a mobile computer. These desktops are mobile ones, small, highly effective and convenient.

  5. Wow, this piece of witing is good, my sister is analyzing these things, thujs I am going to convey her.

  6. Guide To 18 Wheeler Accident Attorneys: The Intermediate Guide For 18 Wheeler Accident Attorneys 18 Wheeler Accident Attorneys

  7. Heya! I’m at work browsing your blog from my new iphone! Just wanted to say I love reading your blog and look forward to all your posts!
    Keep up the excellent work!

    Also visit my blog – Link Login Alt Dor77

  8. Ten Stereotypes About Best American-Style Fridge Freezer That Don’t Always Hold zackfoxworth.top

  9. The 10 Most Terrifying Things About Online Shopping Uk Online Shopping uk

  10. Five Killer Quora Answers To Veterans Disability Attorneys
    Veterans disability attorney

  11. After exploring a number of the blog posts on your site,
    I truly like your technique of writing a blog. I book marked it
    to my bookmark website list and will be checking back soon. Please check out my website too and tell me your opinion.

  12. 15 Gifts For The Railroad Injuries Attorneys Lover In Your
    Life railroad injuries lawsuits (http://dongnampack.co.kr/bbs/board.php?bo_table=free&wr_Id=235271)

  13. Giuseppe表示:

    Upvc Front Doors: The Good, The Bad, And The Ugly lock upvc door (Giuseppe)

  14. Spot on with this write-up, I absolutely think this amazing site needs much more attention. I’ll probably be back again to see more,
    thanks for the information!

    Feel free to visit my web-site :: 777 Slot Online Gacor Dor77

  15. Valerie表示:

    The Hidden Secrets Of Window Repair Near upvc window
    repairs (Valerie)

  16. 7 Things About Personal Injury Attorneys You’ll Kick Yourself For Not Knowing personal injury lawsuit – dmonster506.dmonster.kr,

  17. This website was… how do I say it? Relevant!!
    Finally I have found something which helped me. Many thanks!

  18. 15 Startling Facts About Dangerous Drugs That You Never Knew Dangerous Drugs Lawsuits

  19. whoah thiѕ blog is fangastic i rеally like reading your posts.
    Keep up the great work! You realize, a lot оf individuals aгe searching aroսnd for tһiѕ info, yοu can aid them greatly.

  20. Randy表示:

    How To Ship To Ireland From Uk Techniques To Simplify Your Everyday Lifethe Only How To Ship To Ireland From Uk Trick Every Person Should Learn how to ship to ireland from uk (Randy)

  21. Window Repair Near Me Tools To Streamline Your Everyday Lifethe Only
    Window Repair Near Me Trick That Should Be Used By Everyone Be Able To window repair near me

  22. Hi i am kavin, its my first time to commenting anyplace, when i read this article i thought i could also create
    comment due to this sensible paragraph.

  23. look at here表示:

    I love what you guys tend to be up too. Such clever work and exposure!
    Keep up the great works guys I’ve incorporated you guys to my blogroll.

發佈留言

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