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,430,397 Responses

  1. treadmill表示:

    5 Killer Quora Answers To Treadmills At Home treadmill

  2. آلیسون表示:

    These are actually great ideas in about blogging.

    You have touched some pleasant factors here.

    Any way keep up wrinting.

  3. The Leading Reasons Why People Achieve In The Cribs Industry Best rated cribs

  4. DevonPhack表示:

    https://mexicanpharm24.cheap/# medication from mexico pharmacy
    best drug for ed

  5. LeroyBak表示:

    reputable mexican pharmacies online Mexican pharmacy ship US medicine in mexico pharmacies

  6. Jorgeagopy表示:

    buying prescription drugs in mexico: Legit online Mexican pharmacy – mexican mail order pharmacies

  7. Thank you, I have recently been searching for information approximately this topic for ages and yours
    is the best I’ve discovered so far. However, what about the bottom line?
    Are you positive about the supply?

  8. KevinFiels表示:

    Find medicine information. Latest pill developments.
    pharmacies online prescriptions
    Drug facts here. Drug facts provided.

  9. It’s in point of fact a nice and helpful piece of info.

    I’m happy that you just shared this useful information with us.
    Please keep us up to date like this. Thanks for sharing.

  10. 10 Of The Top Facebook Pages Of All-Time About Treadmill
    That Folds Up Folding Treadmill

  11. LeroyBak表示:

    pharmacy website india India pharmacy delivery india pharmacy mail order

  12. WngPaish表示:

    When you are looking to online pharmacy no prescription percocet for an extended period?Check online to find the best place to

  13. Jorgeagopy表示:

    reputable indian online pharmacy: medicines from India – indian pharmacy

  14. LeroyBak表示:

    what causes ed affordable medication ed cure

  15. DevonPhack表示:

    https://mexicanpharm24.cheap/# mexican online pharmacies prescription drugs
    canadian drugstore online

  16. Williamagric表示:

    over the counter ed drugs https://mexicanpharm24.cheap/# reputable mexican pharmacies online

  17. Kazerafor表示:

    Забудьте про отказы! Новые малоизвестные МФО выдают до 100 000 рублей под низкий процент абсолютно всем. Получите помощь от наших экспертов и подайте заявку без ошибок. Подробнее о выгодных предложениях здесь.

  18. Williamagric表示:

    best erection pills http://indianpharm24.pro/# indian pharmacy online

  19. I really like your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do
    it for you? Plz reply as I’m looking to design my own blog and
    would like to know where u got this from. thanks

  20. LeroyBak表示:

    pharmacy website india Indian pharmacy to USA online shopping pharmacy india

  21. Williamagric表示:

    pharmacy medications http://mexicanpharm24.cheap/# mexico drug stores pharmacies

  22. Jorgeagopy表示:

    mexican border pharmacies shipping to usa: mexican pharm 24 – pharmacies in mexico that ship to usa

  23. Hi there! This is my first visit to your blog! We are a
    collection of volunteers and starting a new project in a community in the
    same niche. Your blog provided us beneficial information to work on. You have done a marvellous job!

  24. Jorgeagopy表示:

    п»їbest mexican online pharmacies: mexico pharmacy – mexican online pharmacies prescription drugs

  25. DevonPhack表示:

    http://mexicanpharm24.cheap/# mexican pharmaceuticals online
    errectile disfunction

  26. Yes! Finally someone writes about Kerafen.

發佈留言

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