0

I have a array which looks like this :-

a[53]={ARPNUM-T,
OR1PATTYP-T,
IVNUM-T,
IVDESC-T,
ORDEPT-T,
ARPNAME-T,
ARGNAME-T,
ARPATADDR1-T,
ARPATCITY-T,
ARPATSTATE-T,
ARPATZIP-N,
ARSEX-T,
ARBIRTH-N,
ARSSN-T,
ARMARRY-T,
ARADMDT-N,
ARDISDT-N,
ARPEMP-T,
ARPHY1-T,
ARPHYNM1-T,
ARMRNUM-T,
ARGUARSSN-T,
ARPHONE-T,
AREMPLYR-T,
ARADDR1-T,
ARSTATE-T,
ARZIP-N,
ARPATPHONE-N,
ARDIAG01-T,
ISSUBNAME-T,
ISCOMPCD-T,
ISCONAME-T,
ISCONTRAC-T,
ISGROUP-T,
ISPRIMARY-T,
ISCOADDR-N,
ISCOCITST-T,
ISPATREL-T,
ISCERTNO-T,
ISCOZIP-N,
ISSUBNAME-T,
ISCOMPCD-T,
ISCONAME-T,
ISCONTRAC-T,
ISGROUP-T,
ISPRIMARY-T,
ISCOADDR-N,
ISCOCITST-T,
ISPATREL-T,
ISCERTNO-T,
ISCOZIP-N,
ARCITY-T}

There are some repeated values like ISSUBNAME-T,ISCOMPCD-T.

i need to fill the array a to array b

where repeated value will be suffixed by the number of times of repetition ,

For eg - if ISSUBNAME-T is repeated 3 times then ISSUBNAME-T_3 .

I have tried a code:-

for (int d = 1; d < 53; d++)
            {

                b[0] = a[0];
                for (int k = 1; k < d; k++)
                {
                    int count = 0;
                    //b[d] = a[d];
                    if (a[d] == a[d - k])
                    {
                        count++;


                        if (count > 0)
                        {
                            b[d] = a[d] + "_" + count + "";
                        }
                        else
                        {
                            b[d] = a[d];
                        }

                        //Console.WriteLine(count);
                    }
                    //Console.WriteLine(count);




                }

                //Console.WriteLine(count);


            }

But it's not showing correct output.

4
  • 2
    "it's not showing correct output" is never enough information. What does it do? It would also be much easier to help you if you'd create a short but complete program demonstrating the problem (with a smaller input, I'd suggest) and format your code more readably. Commented Mar 18, 2014 at 7:14
  • Sorry for the length of the array and everything .. My question was if the elements of array is repeating the program should count it's times of repeatness and suffix that to the other array "b",so that if 1 element is repeating 3 times then first repeated element will be element_1 and element_2 ,element_3 and so on. Commented Mar 18, 2014 at 7:18
  • 2
    Yes, but you haven't said what your current code actually does. You've said that it doesn't work, but that doesn't describe what it does. As I said before, you can make your question much, much better: fix the formatting, reduce the input size, and put this in the form of a short but complete program. You can then show the desired output and your current output. Commented Mar 18, 2014 at 7:20
  • Sure i am doing it now... Commented Mar 18, 2014 at 7:21

1 Answer 1

6

Group array items by their values. Then check if group contains more than one item. If so, then return formatted item value, otherwise simply return item value:

string[] b = a.GroupBy(i => i)
              .Select(g => g.Count() > 1 ?
                           String.Format("{0}_{1}", g.Key, g.Count()) : g.Key)
              .ToArray();

Query syntax (easily allows to calculate group length only once):

var query =  from i in a
             group i by i into g
             let count = g.Count()
             select count > 1 ? String.Format("{0}_{1}", g.Key,count) : g.Key;

string[] b = query.ToArray();

UPDATE: If you want to keep all items and have incremental suffixes

string[] b = a.GroupBy(e => e)
              .SelectMany(g => g.Count() == 1 ?
                               g.Take(1) :
                               g.Select((e,i) => String.Format("{0}_{1}", e,i+1))
              .ToArray();

UPDATE 2: If you want also preserving original order, then simple loop and dictionary will be simpler

string[] b = new string[a.Length];
var duplicatedItems = a.GroupBy(a => a)
                       .Where(g => g.Count() > 0)
                       .ToDictionary(g => g.Key, g => g.Count());

for(int i = b.Length - 1; i >= 0 ; i--)
{
    string item = a[i];
    if (!duplicatedItems.ContainsKey(item))
    {
        b[i] = item;
        continue;
    }

    b[i] = String.Format("{0}_{1}", item, duplicatedItems[item]);
    duplicatedItems[item]--;       
}

Linq query for comparison

string[] b = 
    a.Select((e,i) => new { Item = e, Index = i })
     .GroupBy(x => x.Item)                  
     .SelectMany(g => g.Count() == 1 ?
                      g.Take(1) :
                      g.Select((x,i) => new { 
                            Item = String.Format("{0}_{1}", x.Item, i+1), 
                            Index = x.Index
                      }))
     .OrderBy(x => x.Index)
     .Select(x => x.Item)
     .ToArray();
Sign up to request clarification or add additional context in comments.

5 Comments

2 things : the size of b is less than a i.e. b.length = 42,whereas a=53 And the suffixes are starting with _2 but it should be _1 because its is repeated only once.
k the suffixes i corrected by giving count-1 in the query but the size of b is less than size of a which is a issue
@VivekSaurav I thought you need single item with suffix of same items count. Now its clear what you want. Updated answer
only thing is the output array elements is not in correct sequence when checked with input array elements.
:Thanks a lot for your time and patience

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.