Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7501911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:41:30+00:00 2026-05-29T20:41:30+00:00

I am looking for a concise JavaScript function which takes some values and returns

  • 0

I am looking for a concise JavaScript function which takes some values and returns the unique values sorted by the number of occurrences, the most frequent first.

For example, if the input is the array [3,2,2,2,2,1,2,1] then the output should be [2,1,3].

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:41:31+00:00Added an answer on May 29, 2026 at 8:41 pm

    Here’s my first stab. I bet we can make it simpler, but this seems okay.

     function fancyUnique(arr) {
    
       var counts = {}; // store counts for each value
       var fancy = []; // put final results in this array
       var count = 0; // initialize count
    
       // create counts object to store counts for each value of arr
       for (var i = 0; i < arr.length; i++) {
         count = counts[arr[i]] || 0;  // use existing count or start at 0
         count++; // increment count
         counts[arr[i]] = count; // update counts object with latest count
       }
    
       // take all keys from counts object and add to array
       // also: object keys are string, so must parseInt()
       for (var key in counts) {
         fancy.push(parseInt(key, 10)); 
       }
    
       // sort results array in highest to lowest order
       return fancy.sort(function(a, b) { 
         return counts[b] - counts[a]; 
       })
     }
    
    
    fancyUnique([22,22,1,1,1,1]) // [ 1, 22 ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a concise and functional style way to apply a function to
I've made a function (in JavaScript) that takes an URL from either YouTube or
Python newb here looking for some assistance... For a variable number of dicts in
I am looking for the most concise amount of code possible that can be
I am looking for a concise way to map a variadic function across multiple
I am looking for a concise way to check a value to see if
I'm looking for a clear, concise and accurate answer. Ideally as the actual answer,
I'm just looking for a simple, concise explanation of the difference between these two.
Looking for C# class which wraps calls to do the following: read and write
How do I write a function in C++ that takes a string s and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.