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,227,645 Responses

  1. ClydePab表示:

    matadorbet.bid: matadorbet – matadorbet bid

  2. 15 Things You Didn’t Know About Adhd Assessments Adhd Assessment Cost Uk

  3. Incredible! This blog looks exactly like my old one!
    It’s on a totally different subject but it has pretty much the same
    layout and design. Outstanding choice of colors!

  4. Diplomi_qfMl表示:

    аттестат об окончании 9 классов купить аттестат об окончании 9 классов купить .

  5. HassanbuifS表示:

    http://slot-tr.online/# en kazancl? slot oyunlar?
    deneme bonusu veren siteler yeni

  6. 8 Tips For Boosting Your Pragmatic Slot Experience Game 슬롯 (Ondashboard.Win)

  7. LarryCoell表示:

    slot tr online slot siteleri slot siteleri

  8. LarryCoell表示:

    ultrabet ultrabet bonus ultrabet

  9. Lakesha表示:

    14 Businesses Doing A Great Job At Private Adult Adhd Assessment adhd
    assessment glasgow – Lakesha

  10. LarryCoell表示:

    ultrabet ultrabet ultrabet

  11. Diplomi_qnkn表示:

    купить диплом зоотехника купить диплом зоотехника .

  12. We’re a group of volunteers and opening a brand new scheme
    in our community. Your site offered us with valuable information to work on. You’ve
    done an impressive job and our whole community might be thankful to you.

  13. child porn表示:

    Asking questions are really good thing if you are not understanding anything entirely, except
    this piece of writing presents good understanding yet.

  14. It’s really very complex in this busy life to listen news on Television, so I simply
    use web for that reason, and take the most up-to-date information.

  15. Greetings from Colorado! I’m bored at work so I decided
    to browse your blog on my iphone during lunch break.
    I love the info you present here and can’t wait to take a look when I get home.

    I’m amazed at how quick your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyhow, fantastic site!

  16. LarryCoell表示:

    ultrabet yeni giris 1125 ultrabet yeni giris 1125 ultrabet tr online

  17. Тут можно преобрести сейфы для оружия цены сейф для оружия ружья

  18. Тут можно преобрести купить оружейный шкаф сейф оружейный доставка

發佈留言

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