Thread: get popular number from an array.

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    17

    Lightbulb get popular number from an array.

    Hi everyone,
    I have an array like : a = [3,3,4,3]
    so a[2] = 4 is an exception, I want to get another b, which b = 3 (popular value of a). Which is the best method to do that in the case a has many value inside.
    Thank you !

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    One way could be to use a std::map or std::unordered_map to map numbers to their current counts in the array. You then pick the number with the highest count.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-04-2011, 10:43 PM
  2. Why do you think C# isn't popular?
    By ammar in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 12-05-2002, 03:44 PM
  3. Why is C still so popular ?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 09:26 AM

Tags for this Thread