Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Distinct values in 2D char array...

Distinct values in 2D char array...

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structurestutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adeel Chaudhry
    wrote on last edited by
    #1

    Hello! We have 2D array, Data[20][10], it contains some values, want some suggestions on how to count the distinct values in the array, without getting into 19 loops from 0 to 19 checking against each value!!! We are using VC++ 6. Thanks Adeel --

    C M 2 Replies Last reply
    0
    • A Adeel Chaudhry

      Hello! We have 2D array, Data[20][10], it contains some values, want some suggestions on how to count the distinct values in the array, without getting into 19 loops from 0 to 19 checking against each value!!! We are using VC++ 6. Thanks Adeel --

      C Offline
      C Offline
      Cyrilix
      wrote on last edited by
      #2

      What is the range of your values? If it's not a huge range (more than 0-1000000), you can use a bucket sort technique, which is to basically initialize an array of bools of size 1000000, and set all initial values to false (I'm not sure what values they default to). Then, iterate through all the data in your 2D array and for each value, check if the bool array entry corresponding to the value is true. If it is, then you know you have duplicate values. If it's false, then set the bool array entry to true, so next time, it will detect a duplicate value. Example: If the current iteration of your 2D array gives you a value of 5, you set boolArray[5] = true. If you wish to count the values, then you can use an int array instead of a bool array and just increment as you go.

      1 Reply Last reply
      0
      • A Adeel Chaudhry

        Hello! We have 2D array, Data[20][10], it contains some values, want some suggestions on how to count the distinct values in the array, without getting into 19 loops from 0 to 19 checking against each value!!! We are using VC++ 6. Thanks Adeel --

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        madeelch1986 wrote:

        We have 2D array, Data[20][10], it contains some values, want some suggestions on how to count the distinct values in the array

        Stealing from Cyrilix, here's one way...

        char Data[20][10];

        // ... initialize Data[][] here ...

        bool Flags[256];

        memset(Flags, 0, sizeof(Flags));

        int UniqueCount = 0;

        for (int i = 0; i < 20; i++)
        {
        for (int j = 0; j < 10; j++)
        {
        if (false == Flags[(unsigned char)Data[i][j]])
        {
        Flags[(unsigned char)Data[i][j]] = true;
        ++UniqueCount;
        }
        }
        }

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups