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,232,367 Responses

  1. Vince表示:

    Why Sports Toto Check Winning Will Be Your Next Big Obsession 먹튀검증 – Vince

  2. También puedes usar los videos de TikTok
    para fines de investigación o marketing.

  3. Good way of describing, and nice paragraph to obtain information about my presentation focus,
    which i am going to convey in school.

    My webpage :: คาสิโนออนไลน์

  4. Don’t Forget Situs Togel Dan Slot Terpercaya: 10 Reasons Why You Don’t Really Need It Situs Togel Online

  5. Derekfar表示:

    matadorbet: matadorbet – matadorbet.bid

  6. The 9 Things Your Parents Taught You About Walking Pad For Desk
    walking pad for desk (images.google.be)

  7. It is appropriate time to make some plans for the long run and it
    is time to be happy. I have read this submit and if I may I want to counsel you few
    attention-grabbing things or tips. Perhaps you could write
    next articles regarding this article. I desire to read even more things about it!

    https://w2.pencariangka.website/

  8. Nichole表示:

    5 Killer Quora Answers On Car Key Cutting And Programming car key cutting and
    programming (Nichole)

  9. 10 Untrue Answers To Common Oak Electric Fireplace
    Suites Questions: Do You Know The Right Answers? Free standing fireplace mantel

  10. 10 Car Locksmith Near Me That Are Unexpected Mobile Locksmiths For Cars

  11. Jonna表示:

    20 Things You Need To Be Educated About Three Wheel Rollators With Seat storage 3 wheel rollator, Jonna,

  12. ClydePab表示:

    matadorbet.bid: matadorbet – matadorbet

  13. This is my first time go to see at here and i am truly pleassant to read everthing at single place.

  14. Godfather789表示:

    Godfather789ยเว็บตรงของคนไทย เจ้าพ่อมาเฟียเว็บใหญ่ไม่มีโกง

  15. HassanbuifS表示:

    http://denemebonusuverensiteler.top/# deneme bonusu veren siteler betturkey betturkey.com
    deneme bonusu veren siteler betturkey

  16. HassanbuifS表示:

    http://denemebonusuverensiteler.top/# deneme bonusu veren siteler yeni
    deneme bonusu veren siteler

  17. pornbasis.com表示:

    whoah this blog is fantastic i love reading your articles.
    Keep up the good work! You know, many persons are hunting around for
    this info, you could aid them greatly.

  18. corporate表示:

    Hello! Do you know if they make any plugins to help with SEO?

    I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains.

    If you know of any please share. Thanks!

  19. Тут можно преобрести где купить оружейный сейф оружейные сейфы и шкафы для ружей

發佈留言

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