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

805,636 Responses

  1. all in lotto表示:

    Excellent, what a web site it is! This web site presents valuable data to us,
    keep it up.

  2. Online betting has a number of advantages for both bookmakers and bettors.
    Online betting sites permit people to place bets
    on their favorite sports, as well as earn them money.
    These sites offer many ways to deposit funds.

    Also visit my webpage live – http://mgri-rggru.ru/

  3. QQSutera表示:

    Thanks very interesting blog!

    Check out my homepage; QQSutera

  4. Aw, this was a really good post. Spending some time and actual effort
    to produce a great article… but what can I say… I hesitate a whole lot and don’t manage to get anything done.

  5. betting表示:

    The popularity of online gambling has increased in the last decade.
    In 1996, there were just fifteen websites. In 1997 there were over 200 websites.

    Feel free to visit my site: betting

  6. Gambler表示:

    There are many reasons to get involved in betting on sports.
    From the possibility of making a profit to the excitement and the chance to root for your favorite team in addition to the
    overall experience.

    Feel free to surf to my website Gambler

  7. Brandendooky表示:

    dark web search engines blackweb

  8. gambler表示:

    Sports fans in New York are allowed to place bets
    online starting January. 8 2022. Four major operators
    have been granted licenses for mobile betting.
    They will be able offer their services in a variety of ways.

    My homepage: gambler

  9. dealers表示:

    There are many benefits to playing at an online casino.
    Signing up doesn’t require you to spend a lot of money.

    Most online casinos offer free games, and players can test them
    to determine what games they like the best.

    Check out my web page – dealers

  10. Wow that was unusual. I just wrote an really long comment but after I clicked
    submit my comment didn’t appear. Grrrr… well I’m not writing all that
    over again. Regardless, just wanted to say excellent blog!

  11. Greetings! Very helpful advice within this article!
    It’s the little changes that will make the most significant changes.
    Thanks a lot for sharing!

    Here is my blog – chosungreen.softedu.co.kr

  12. Online poker offers many advantages. One of them is that there are
    no waiting lists, no second-hand smoke, and you can play any time you’d like.
    You can also enjoy many different games.

    Stop by my blog post :: dealers (incrediblepanels.com)

  13. 20tools.com表示:

    Betting on online sports betting gives bookmakers and bettors many benefits.

    Apart from allowing gamblers to place bets on their favorite sports online betting
    sites allow them to earn money. They provide a wide range
    of options for depositing money.

    My site: Casino – 20tools.com,

  14. Howdy! Would you mind if I share your blog with my facebook group?
    There’s a lot of folks that I think would really enjoy your content.
    Please let me know. Thank you

  15. Georgiana表示:

    The Internet has opened up a variety of new opportunities for people who love
    to play. The popularity of online poker has grown dramatically in recent years.

    Feel free to surf to my web-site; gaming (Georgiana)

  16. best表示:

    The Internet has created a wealth of new opportunities for those who love
    poker. The popularity of online poker has increased significantly in recent
    years.

    Also visit my webpage: best

  17. DavidLek表示:

    cvs prescription prices without insurance comfortis without vet prescription

  18. slots表示:

    Slots online are an increasingly popular pastime for millions of casino players online.
    This thrilling and enjoyable type of gambling is a excellent way to pass the time or
    to make extra money.

  19. can you mix viagra and sleeping pills – https://belviagra.com buy viagra online from uk

  20. DavidLek表示:

    mexican pharmacy without prescription how to get prescription drugs without doctor

  21. Sports表示:

    There are a myriad of rules and regulations that govern online gambling.
    For instance in the US, you cannot have gambling sites that are in your home country.
    However, it could be set up elsewhere.

    Feel free to visit my web-site: Sports

  22. blackjack表示:

    Online gambling has grown in popularity in the last decade.

    In 1996, there were just fifteen websites. In 1997, there were more than 200 websites.
    According to Frost & Sullivan, online gambling
    generated $830 million in revenues in 1998.

    Feel free to visit my page … blackjack

發佈回覆給「London Adhd Clinic」的留言 取消回覆

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