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,375,497 Responses

  1. Dell表示:

    Guide To Tilt And Turn Upvc Windows Buy Online: The
    Intermediate Guide The Steps To Tilt And Turn Upvc Windows
    Buy Online tilt and turn upvc windows buy online, Dell,

  2. The 10 Worst Robotic Vacuum Cleaner On Sale Failures Of All Time Could Have Been Prevented robot vacuum Cleaner

  3. Thanks for sharing these tips on water removal! I’ll definitely check out water damage restoration for more info

  4. 10 Startups Set To Change The Retro Style Fridge Freezer Industry For The
    Better retro style Fridge freezers

  5. OP表示:

    After Hours OP

  6. Awesome blog you have here but I was wanting
    to know if you knew University of Michigan collegiate fellowship any
    message boards that cover the same topics talked about in this article?
    I’d really love to be a part of community where I can get responses from other
    experienced individuals that share the same interest.
    If you have any recommendations, please let me know. Appreciate it!

  7. I really like reading a post that will make men and women think.
    Also, thanks for allowing for me to comment!

  8. Patrick Lyons表示:

    I recently started a flower garden, and it’s been so fulfilling! The pleasure of nurturing plants is something everyone ought to experience. Locate pointers on gardening at same day flower delivery

  9. Power Tools Store Tips That Will Revolutionize Your Life buying power tools online

  10. read here表示:

    Howdy, I think your blog might be having web browser compatibility problems.
    When I take a look at your web site in Safari, it looks fine but when opening in Internet Explorer,
    it has some overlapping issues. I just wanted to give you a quick
    heads up! Other than that, great blog!

  11. sga123表示:

    I loved as much as you’ll receive carried out right here.

    The sketch is attractive, your authored material stylish.
    nonetheless, you command get bought an shakiness over that
    you wish be delivering the following. unwell unquestionably come further formerly
    again as exactly the same nearly a lot often inside case you shield this hike.

  12. Emily Clark表示:

    Thanks for sharing these tips on deck building! They will definitely help me in my next project deck builder

  13. سلام دوست عزیز! مقاله خیلی آموزنده
    بود و من از این سایت کلی اطلاعات به دست آوردم.
    توصیه می‌کنم همه اینجا رو امتحان کنن و
    از خدمات با کیفیت شما بهره‌مند بشن.
    همچنین پیشنهاد می‌کنم برای بهینه‌سازی سئو سایت خود، بک‌لینک‌های
    سایدبار از virgool.io را خریداری
    کنید. برای ارتباط با من به تلگرام @myd_80 مراجعه کنید.
    ما یک گروه سئو پرم در تلگرام داریم و پست‌های مهمان با کیفیت برای فروش ارائه
    می‌دهیم.

  14. Greetings! Very helpful advice in this particular post! It’s the little changes that make the greatest changes.
    Thanks for sharing!

  15. I read this paragraph completely concerning the resemblance of most up-to-date and earlier technologies, it’s amazing article.

  16. Asphalt paving seems so much easier than I thought after reading this article! Visit driveway installation for more

  17. I don’t know if it’s just me or if perhaps everybody else experiencing issues
    with your website. It appears like some of the written text within your content are running off the screen.
    Can someone else please provide feedback and let me know if this
    is happening to them too? This may be a issue with
    my browser because I’ve had this happen before. Many thanks

    Feel free to surf to my web blog … Erasmus Mundus scholarship

  18. Norman Patton表示:

    Monuments serve as a reminder of our past and the people who shaped it. Discover fascinating monument designs at tombstone company

發佈留言

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