Topic
3 replies Latest Post - ‏2012-11-20T11:53:02Z by MohanRS
MohanRS
MohanRS
6 Posts
ACCEPTED ANSWER

Pinned topic unique array determination

‏2012-11-19T18:44:26Z |
Hi friends,

I have an array

string arr[7]={"Apple","orange","orange","pineapple","Apple","Apple","orange"}

i like output to printed

Apple - 3
orange -3
pineapple -1

can you please help.
Updated on 2012-11-20T11:53:02Z at 2012-11-20T11:53:02Z by MohanRS
  • llandale
    llandale
    2939 Posts
    ACCEPTED ANSWER

    Re: unique array determination

    ‏2012-11-19T20:02:37Z  in response to MohanRS
    //*********************
    bool fPutSkipIncrement(Skip skpList, string KeyValue)
    { // Insert the KeyValue into the Skip list's 'Key'.
    // The 'Data' portion of the Skip is set to 1 for new inserts and is
    // incremented for already existing 'Key's in the Skip.
    // 'Data' MUST be of type Integer.
    // (represents the number of references).
    // Return whether this is a first insert for KeyValue into the Skip.

    bool IsNewValue = false

    int NumReferences = 0 // Remains zero if next "find" doesn't find anything:
    if(find(skpList, KeyValue, NumReferences))
    delete(skpList, KeyValue) // Remove It
    else IsNewValue = true // Mark it New
    // ReInsert new or updated counts
    NumReferences++
    put (skpList, KeyValue, NumReferences)
    return(IsNewValue)
    } // end fPutSkipIncrement()

    // ========== MAIN =============
    string arr[7]={"Apple","orange","orange","pineapple","Apple","Apple","orange"}

    int i, Count
    string Fruit
    Skip skpTotals = createString() // KEY 'string' fruit; DATA 'int' count

    for (i=0; i<sizeof(arr); i++)
    { fPutSkipIncrement(skpTotals, arr[i])
    }
    for Count in skpTotals do
    { Fruit = (string key skpTotals)
    print Count "\t" Fruit "\n"
    }
    delete(skpTotals)
    • llandale
      llandale
      2939 Posts
      ACCEPTED ANSWER

      Re: unique array determination

      ‏2012-11-19T20:03:32Z  in response to llandale

      Oops

      //*********************
      bool    fPutSkipIncrement(Skip skpList, string KeyValue)
      {     // Insert the KeyValue into the Skip list's 'Key'.
              // The 'Data' portion of the Skip is set to 1 for new inserts and is
              //    incremented for already existing 'Key's in the Skip.
              // 'Data' MUST be of type Integer.
              //   (represents the number of references).
              // Return whether this is a first insert for KeyValue into the Skip.
       
              bool    IsNewValue = false
       
              int     NumReferences = 0       // Remains zero if next "find" doesn't find anything:
              if(find(skpList, KeyValue, NumReferences))
                   delete(skpList, KeyValue)          // Remove It
              else IsNewValue = true                  // Mark it New
                              // ReInsert new or updated counts
              NumReferences++
              put (skpList, KeyValue, NumReferences)
              return(IsNewValue)
      }     // end fPutSkipIncrement()
       
       
       
       
       
      string arr[7]={"Apple","orange","orange","pineapple","Apple","Apple","orange"}
       
      int     i, Count
      string  Fruit
      Skip    skpTotals = createString() // KEY 'string' fruit; DATA 'int' count
       
      for (i=0; i<sizeof(arr); i++)
      {  fPutSkipIncrement(skpTotals, arr[i])
      }
      for Count in skpTotals do
      {  Fruit = (string key skpTotals)
         print Count "\t" Fruit "\n"
      }
      delete(skpTotals)
      
      Updated on 2014-01-09T10:34:20Z at 2014-01-09T10:34:20Z by iron-man
      • MohanRS
        MohanRS
        6 Posts
        ACCEPTED ANSWER

        Re: unique array determination

        ‏2012-11-20T11:53:02Z  in response to llandale
        It Works!!! Thanks llandale