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,552 Responses

  1. Hey I know this is off topic but I was wondering if you
    knew of any widgets I could add to my blog that automatically
    tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and
    was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly
    enjoy reading your blog and I look forward to your new
    updates.

  2. Content-Spinning.fr propose des outils pour transformer facilement un paragraphe en une meilleure
    version, en ajoutant des mots et des phrases variées,
    sans perte de sens. Notre réformulateur peut vous aider à améliorer votre contenu.

  3. Dereketerm表示:

    how to buy generic clomid for sale: rex pharm – buy generic clomid no prescription

  4. Led ma trận表示:

    We stumbled over here by a different web address and thought I should check things out.
    I like what I see so now i am following you. Look forward to going over your web page for a second time.

  5. Good info. Lucky me I found your website by accident
    (stumbleupon). I’ve bookmarked it for later!

  6. Markdob表示:

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

  7. StevenNub表示:

    priligy: dapoxetine online – cheap priligy

  8. BrandenSlomy表示:

    where to get generic clomid without dr prescription: how can i get clomid no prescription – buy generic clomid

  9. Markdob表示:

    Приветствую. Может кто знает, где найтиполезные блоги о недвижимости? Пока нашел – https://disk-pila.ru

  10. Markdob表示:

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

  11. bdsm room表示:

    I was recommended this blog by way of my cousin. I’m
    now not sure whether or not this put up is written by him as no one
    else know such specific about my problem. You are incredible!
    Thanks!

  12. StevenNub表示:

    dapoxetine online: dapoxetine price – cheap priligy

  13. StevenNub表示:

    prednisone for sale no prescription: buy prednisone – prednisone over the counter cost

  14. Markdob表示:

    Приветствую. Подскажите, где найтиразные блоги о недвижимости? Сейчас читаю – https://disk-pila.ru

  15. pecah perawan表示:

    I enjoy what you guys are up too. This sort of clever work and coverage!

    Keep up the great works guys I’ve added you guys to my personal blogroll.

  16. This is a good tip particularly to those fresh to the blogosphere.
    Simple but very accurate information… Appreciate your sharing this one.

    A must read article!

  17. Markdob表示:

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

  18. Highly descriptive article, I enjoyed that
    bit. Will there be a part 2?

  19. free xxx表示:

    Thanks for finally talking about > JS 依照元素出現數量排序 –
    馬老師 雲端研究室 < Loved it!

  20. StevenNub表示:

    prednisone 5 mg tablet price: 10 mg prednisone – prednisone 10mg prices

  21. Nikefut表示:

    darkweb marketplace dark market onion https://darknetmarketstore.com/ – darkmarket

發佈留言

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