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...

965,399 Responses

  1. Archer表示:

    Value Your Insights!

    I’m Thrilled you Came across the Entry Informative.

    If you’re Interested in Probing more Options in the online Bookmaking
    Landscape, I’d Recommend Checking out CMD368.

    They Offer a Wide range of Thrilling Punting Choices, Livestream, and a Accessible App.

    What I Highly Admire about CMD368 is their Devotion to Sensible Gaming.
    They have Comprehensive Policies and Functionalities to Support Individuals Manage
    their actions.

    Regardless if you’re a Seasoned Punter or Unaccustomed to the
    Wagering, I Imagine you’d Truly Cherish the Venture.

    Feel free to Sign up Using the URL and Ask if you have More Doubts.

    Here is my blog – cmd368 singapore (Archer)

  2. Apprenez à devenir autonome et à économiser sur les frais de vidange et de pompage en suivant les conseils simples fournis par le site.

  3. MelissaUnult表示:

    Восполнить пробел?
    this necessity is oriented at attracting https://green-bitcoin.app/ on a daily basis, which increases the consistency and growth of the network. By the end of 2024, the value of the token may rise to one.6 dollars due to the solid community base, strategic marketing efforts and the ongoing attractiveness of sustainable blockchain-based platforms.

  4. Watch Out: What Shop Online Uk Women’s Fashion Is Taking Over
    And What We Can Do About It 220V Analytical Scale

  5. Why Adding A Truck Accident Attorney To Your Life Will Make All The Impact Truck Accident attorneys

  6. Thanks for your personal marvelous posting!
    I quite enjoyed reading it, you could be a great
    author. I will make sure to bookmark your blog and may come back at
    some point. I want to encourage you to continue your great writing, have a nice weekend!

  7. Medicament information. Long-Term Effects.
    where to buy generic celebrex
    All information about drug. Read now.

發佈留言

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