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,238,241 Responses

  1. AndrewPhice表示:

    Арматура — это металлический прут, используемый для армирования железобетонных конструкций. Стержни арматуры изготавливаются из углеродистой металлическая арматура цена за тонну

  2. If you wоuld like to take a grеat deal from this piece of writing thеn yoս hɑѵe to apply
    thеse strategies tߋ your won weblog.

    Alѕo visit my page – Rajabandot Login

  3. Mesothelioma lawyers aid families and victims throughout the legal process.
    They create cases, file lawsuits and Asbestos settlement trust claims.
    They also negotiate settlements and fight for a fair trial.

  4. Jorgeagopy表示:

    aspirin and ed: low cost pharmacy – canada ed drugs

  5. Bobbye表示:

    10 Inspiring Images About Sports Toto Official Website
    메이저사이트 (Bobbye)

  6. Experience hassle-free software installation and unparalleled support with Software Sale Mart.

  7. What To Say About Latest Sport Toto Result To Your Mom 토지노 커뮤니티

  8. Titration ADHD Tools To Improve Your Daily Life Titration ADHD Trick That Should Be Used By Everyone Be Able To Titration adhd

  9. Greetings! Very useful advice within this post!

    It is the little changes that make the most significant changes.
    Many thanks for sharing!

    https://w2.paito.click/

  10. See What Glazing Doctor Tricks The Celebs Are Making Use Of Glazing Doctor

  11. 20 Things You Must Be Educated About Vauxhall Car Key Replacement Vauxhall Meriva Car Key Replacement

  12. I’ve been exploring for a bit for any high quality articles or weblog posts on this kind of house .

    Exploring in Yahoo I ultimately stumbled upon this web site.
    Studying this info So i am satisfied to convey that I’ve
    a very just right uncanny feeling I came upon just what I
    needed. I such a lot surely will make certain to do not omit this web site and
    provides it a glance on a continuing basis.

  13. Williamagric表示:

    best ed solution http://mexicanpharm24.cheap/# reputable mexican pharmacies online

  14. Hello to all, how is the whole thing, I think every one
    is getting more from this site, and your views are good for new viewers.

  15. rust cheats表示:

    I read this article fully about the comparison of newest and earlier technologies, it’s remarkable
    article.

  16. slot gacor表示:

    That is very interesting, You’re an overly professional blogger.
    I’ve joined your feed and sit up for searching for more of your great
    post. Additionally, I have shared your web site in my social networks

  17. BrandonSaisy表示:

    cheap medication: cheap drugs online – buy cheap prescription drugs online

  18. This is my first time pay a visit at here and i am actually happy to read
    everthing at one place.

  19. The 10 Most Popular Pinterest Profiles To Keep Track Of Window
    Handles Install Window handle

  20. Jorgeagopy表示:

    best online pharmacies in mexico: mexico pharmacy cheap – buying prescription drugs in mexico online

  21. DevonPhack表示:

    http://pharm24.pro/# ed medication online
    new erectile dysfunction treatment

  22. DevonPhack表示:

    http://pharm24.pro/# can ed be cured
    buy prescription drugs from india

發佈留言

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