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,245,365 Responses

  1. DarrenUnjuh表示:

    http://plavixclo.com/# plavix best price

  2. вывод из запоя на дому в краснодаре http://mozaisk.anihub.me/viewtopic.php?id=4370 .

  3. Nice post. I was checking constantly this weblog and I’m impressed!
    Extremely helpful information particularly the final
    part 🙂 I care for such information a lot. I used to be looking
    for this particular information for a very long time.
    Thank you and best of luck.

  4. Ninfa Fan表示:

    Converting Land to plantation

  5. Markdob表示:

    Приветствую. Подскажите, где почитатьразные статьи о недвижимости? Пока нашел – https://opalubki26.ru

  6. Markdob表示:

    Всем привет! Может кто знает, где найтиразные статьи о недвижимости? Сейчас читаю – https://opalubki26.ru

  7. DarrenUnjuh表示:

    https://lisinopril1st.com/# buy Lisinopril online

  8. BernardMop表示:

    cheapest Lisinopril lisinopril 3760 buy Lisinopril online

  9. Hello there, just became aware of your blog through Google, and found that it’s truly informative.
    I am going to watch out for brussels. I will be grateful if you continue this in future.
    Many people will be benefited from your writing. Cheers!

  10. вывод из запоя цены на дому краснодар http://zelenograd.rusff.me/viewtopic.php?id=1232 .

  11. blog link表示:

    Howdy! I simply would like to offer you a big thumbs up for the great information you have right here on this post.
    I’ll be returning to your website for more soon.

  12. Markdob表示:

    Всем привет! Может кто знает, где почитатьразные статьи о недвижимости? Сейчас читаю – https://opalubki26.ru

  13. Excellent article et très informatif! Les services de gestion en ligne offrent une commodité incroyable pour
    les entreprises. Merci pour le partage de cet article et pour les conseils pratiques!

  14. Markdob表示:

    Приветствую. Может кто знает, где найтиполезные статьи о недвижимости? Сейчас читаю – https://opalubki26.ru

  15. AlbertDuM表示:

    buy cytotec: Cytotec 200mcg price – Misoprostol 200 mg buy online

  16. Rickie Allain表示:

    Reforestation for plantation of Agriculture Land

  17. Thanks for sharing your thoughts about whatsapp電腦版登入.

    Regards

  18. вывод из запоя краснодар на дому анонимно https://www.superstar.ukrbb.net/viewtopic.php?f=3&t=682 .

  19. принудительный вывод из запоя краснодар http://www.superstar.ukrbb.net/viewtopic.php?f=3&t=682 .

  20. Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something.
    I think that you could do with a few pics to drive the message home a little bit, but
    other than that, this is wonderful blog. A great read. I’ll
    certainly be back.

  21. DarrenUnjuh表示:

    https://plavixclo.com/# buy clopidogrel bisulfate

  22. Immediate Mix表示:

    Everyone loves what you guys are usually up too. Such clever work and reporting!
    Keep up the amazing works guys I’ve added you guys to my blogroll.

  23. Узнай все о исправление носовой перегородки ценасептопластика в москве цена

發佈留言

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