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,154,253 Responses

  1. Are You Tired Of Upvc Windows And Doors Near Me? 10 Inspirational Sources That Will Invigorate Your Love upvc windows Doors (Link.uisdc.com)

  2. Kudos, I enjoy this.

  3. Drugs information for patients. Brand names.
    can you buy generic compazine no prescription
    Some news about medication. Get here.

  4. link bokep表示:

    Hi there, I log on to your blog regularly. Your story-telling style is awesome, keep doing what
    you’re doing!

  5. http://rwl.ro表示:

    Five Killer Quora Answers To Window Repair Near Me window Repair near
    me (http://rwl.ro)

  6. Hey there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate!

    He always kept chatting about this. I will forward this article to him.
    Pretty sure he will have a good read. Thanks for sharing!

    Also visit my website; fitspresso contact

  7. Kent casino表示:

    Однако вы должны знать, что снятие средств подлежит проверке Оператором.

  8. eddafay表示:

    10 Times You’ll Have To Be Educated About Bunk Bed Price eddafay

  9. boy789表示:

    Yes! Finally someone writes about boy789.

  10. І everү tіme spent myy half ɑn һour to rread tһіѕ webpage’s articles
    or reviews аll the tіme aong with a mᥙg of coffee.

    My homepage :: Arena333 Slot Login Link Alternatif

  11. Ye’s poetry glows brilliantly on these posters. Each lyric captures the
    essence of his special blend of innovation and cultural relevance.
    To get authentic Kanye devotees, these types of posters are really a must-have!

  12. 10-Pinterest Accounts You Should Follow About Mesothelioma Lawsuit mesothelioma claims

  13. Medicines information sheet. Long-Term Effects.
    can i buy cheap reglan without rx
    Best news about drug. Get here.

  14. Najzanimljivije diskusije o automobilima su u komentarima.
    Citroen Cruze je popularan izbor medu kompaktnim automobilima.
    Auto mi je kao muzej – svaki deo ima svoju pricu.

  15. Winning Slots表示:

    You’ll Never Guess This Winning Slots’s Secrets Winning Slots

  16. Medication information leaflet. Effects of Drug Abuse.
    cheap dilantin no prescription
    Best about meds. Get here.

  17. Vegas Slots表示:

    The 10 Most Terrifying Things About Vegas Slots Vegas Slots

  18. 5 Must-Know Practices For Auto Accident Case In 2023 auto Accident Law firms (http://www.google.com)

  19. memek cantik表示:

    Truly no matter if someone doesn’t be aware of after that its up to other viewers that
    they will assist, so here it occurs.

  20. focusis.co.kr表示:

    5 Interactive Slots Projects For Any Budget jackpot Winners
    slots (focusis.co.kr)

  21. 5 Branded Slots Myths You Should Avoid themed slots (Chlebkova.blog.idnes.Cz)

  22. Drug information sheet. Brand names.
    how to get dramamine without prescription
    Best information about medication. Get now.

  23. Drugs information leaflet. Long-Term Effects.
    loperamide pill
    All about pills. Read now.

  24. The social platform Reddit has become an essential platform for posting information, gaining comments, and growing communities.
    Using its unique upvote and downvote mechanism,
    Reddit allows users to promote trending posts and downvote irrelevant submissions.
    Due to the importance of upvotes in increasing exposure, many people are looking
    for ways to get more upvotes. This has led to the rise of services offering Reddit upvotes and panels where users can buy Reddit upvotes.

    ### Understanding Reddit Upvotes

    Reddit upvotes work like likes on sites. They indicate approval of a post and assist in bringing it
    to the awareness of more users. The more upvotes content receives, the higher its
    probability of being featured on the front page of Reddit or
    even the entire site. Such visibility can lead to higher traffic, interaction, and credibility for
    the user.

    ### Why Buy Reddit Upvotes?

    There are several reasons why people might decide to
    purchase Reddit upvotes. Here are some of the most common motives:

    – **Boost Visibility:** Purchasing upvotes can help making a submission more noticeable to other
    users, particularly in crowded forums.
    – **Build Credibility:** A high number of upvotes can give trustworthiness
    to a post, making it more trustworthy and drawing in more engagement.

    – **Increase Visits:** For users looking to advertise an online platform, service, or
    content, higher upvotes can result in increased traffic and
    possible sales.
    – **Shape Conversations:** High upvotes can shape the direction of discussions and showcase specific
    viewpoints.

    ### Services Offering Reddit Upvotes

    Services for buying Reddit upvotes have emerged to meet the growing demand for positive votes.

    These providers offer clients the opportunity to buy upvotes for a fee, promising to boost the
    visibility and credibility of their posts.

    – **Functionality:** Most Reddit upvote services operate by
    creating an array of profiles that are used to upvote
    content. These accounts might be controlled by bots or
    real people.
    – **Packages and Pricing:** These providers provide various packages based on the number of upvotes one wants
    to buy. Prices differ based on the amount and speed of provision.
    – **Pros and Cons:** While buying Reddit upvotes can boost visibility and credibility,
    it also potential downsides. Employing such services
    can be against Reddit’s rules, resulting in account suspension or post removal.

    ### Platforms for Purchasing Reddit Upvotes

    Platforms dedicated to selling Reddit upvotes provide an easy system for users to
    purchase upvotes quickly and without hassle. These panels offer a variety
    of services to improve the user experience.

    – **Easy-to-Use Platforms:** Most platforms are made to be simple to use, allowing
    users to select their preferred package and make their order with just a few clicks.

    – **Customizable Options:** Clients can customize
    their purchases by choosing the amount of upvotes and how
    quickly they are delivered. This customization lets clients to satisfy their specific requirements.

    – **Protection of Information:** Trusted panels guarantee safety and privacy by using safe
    transactions and protecting user data.

    ### Drawbacks of Buying Reddit Upvotes

    While purchasing Reddit upvotes can offer many advantages, one must think about the potential risks:

    – **Against Site Rules:** Reddit bans manipulating votes,
    and buying upvotes might lead to account suspension or post removal.

    – **No Genuine Interaction:** Votes purchased through providers
    might not lead to real engagement with the post, which can limit its lasting effect.

    – **Ethical Concerns:** Some people think that buying upvotes is cheating,
    as it artificially boosts visibility without organic support from the audience.

    ### Conclusion

    Purchasing Reddit upvotes can be an effective tactic
    for boosting exposure and trustworthiness on the platform, particularly in competitive forums.

    Nevertheless, it is crucial to consider the possible risks and ethical concerns
    before deciding to employ such services. In the end, genuine engagement and quality
    posts stay the best methods to succeed on Reddit.

  25. SEO_foMa表示:

    продвижение сайтов в москве и московской области https://prodvizhenie-sajtov-v-moskve115.ru .

  26. SEO_ryMa表示:

    продвижение сайтов в москве тарифы http://www.prodvizhenie-sajtov-v-moskve115.ru .

  27. Medication prescribing information. What side effects?
    neurontin cost walmart
    Some about pills. Get information now.

  28. SEO_paMa表示:

    seo продвижение сайтов в москве prodvizhenie-sajtov-v-moskve115.ru .

發佈留言

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