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,242,999 Responses

  1. mindful表示:

    Hello I am so excited I found your blog page, I really found you
    by accident, while I was searching on Askjeeve for something else, Regardless I am here now and would just like to say thanks for a
    incredible post and a all round interesting blog (I also love the theme/design), I don’t have time to browse it all at the minute but I have saved
    it and also added in your RSS feeds, so when I have time I will be back to read much
    more, Please do keep up the fantastic work.

  2. google表示:

    Wow that was strange. I just wrote an incredibly long comment but after I
    clicked submit my comment didn’t show up. Grrrr…
    well I’m not writing all that over again. Regardless,
    just wanted to say fantastic blog!

  3. BrandenSlomy表示:

    prednisone 50: cheap prednisone – where can i get prednisone

  4. Nikefut表示:

    tor marketplace dark web search engine https://mydarknetmarketlinks.com/

  5. I couldn’t resist commenting. Well written!

  6. DarrenUnjuh表示:

    https://cytpremium.com/# п»їcytotec pills online

  7. ww88-now表示:

    It’s remarkable to visit this web site and reading the views of all friends on the topic of this article,
    while I am also keen of getting familiarity.

  8. BrandenSlomy表示:

    amoxicillin 250 mg price in india: amoxil com pharm – generic amoxicillin

  9. Medicine information leaflet. What side effects can this medication cause?
    where can i get chlorpromazine pill
    Actual information about medication. Read information here.

  10. click now表示:

    For hottest information you have to pay a visit internet and on web I found this site as a finest
    web site for latest updates.

  11. nodeposit表示:

    It’s appropriate time to make some plans for the longer term and it’s time to be happy.

    I have learn this put up and if I may just I wish to recommend you few attention-grabbing issues or tips.

    Perhaps you can write subsequent articles relating to this article.
    I want to learn even more things approximately it!

  12. It’s truly a great and helpful piece of info.
    I’m satisfied that you simply shared this useful info with us.
    Please stay us up to date like this. Thank you for sharing.

  13. Nikefut表示:

    tor market links tor markets links https://mydarknetmarketlinks.com/

  14. It’s remarkable in favor of me to have a site, which is good in support of my know-how.
    thanks admin

  15. George Valdez表示:

    If you remain in the Phoenix area and wish to enhance your online presence, you ought to certainly look into Phoenix SEO services Digitaleer Phoenix SEO & Web Design

發佈留言

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