none
Manipulation of an Array in C# RRS feed

  • Question

  • I have an string SourceArray which contains items as follows: The source array has tab as delimiter looks like this

    SourceArray[0]="Tom\tis\tcoding";
    SourceArray[1]="Harry\tin\ttown";
    SourceArray[1]="Tom\tworking\thard";

    Now the destinationArray would be like this.

    destinationArray[0]="Tom*is,working";
    destinationArray[1]="Harry*in";

    The application would search the source array and search for string "Tom","Harry" if it find a match then would append the destination array with the match find item i.e. with the second items in the string which are "is" and "working" Else it would add the item in the array, Which best possible way to resolve this problem w.r.t to complexity.

    Monday, December 4, 2017 8:36 PM

All replies

  • Assuming that the list of the names is got from the first word of these strings, check one of possible solutions:

    string[] SourceArray = new string[3];
    
    SourceArray[0] = "Tom\tis\tcoding";
    SourceArray[1] = "Harry\tin\ttown";
    SourceArray[2] = "Tom\tworking\thard";
    
    string[] names =
    	SourceArray
    		.Select( s => s.Split( new[] { '\t' }, 2 ).First() )
    		.Distinct()
    		.ToArray();
    
    string[] destinationArray =
    	names
    		.Select( n => n + "*" + string.Join( ",", SourceArray.Select( s => s.Split( new[] { '\t' }, 3 ) ).Where( a => a[0] == n ).Select( a => a[1] ) ) )
    		.ToArray();
    

    Monday, December 4, 2017 8:59 PM
  • Hello Uddipto,

    Try the following linq example.

     string[] SourceArray = new string[5];
                SourceArray[0] = "Tom\tis\tcoding";
                SourceArray[1] = "Harry\tin\ttown";
                SourceArray[2] = "Tom\tworking\thard";
                SourceArray[3] = "ssssssssssssssssss";
                SourceArray[4] = "Tom\tplay\tgame";
    
                string[] Names = new string[] { "Tom", "Harry" };
    
    
                var result= Names.Select(z => SourceArray.Select(x => x.Split('\t')).Where(y => Names.Contains(y.FirstOrDefault())).GroupBy(x => x.FirstOrDefault().Equals(z))).FirstOrDefault().Select(x => x.Select(z => z[0]).FirstOrDefault() + "*" + string.Join(",", x.Select(y => y[1]))).ToList();

    And it could be divided into 3 parts for easily debugging.

      var step1 = SourceArray.Select(x => x.Split('\t')).Where(z=>Names.Contains(z.FirstOrDefault()));
    
                var step2 = Names.Select(z => step1.GroupBy(x => x.FirstOrDefault().Equals(z))).FirstOrDefault().ToList();
    
                var step3 = step2.Select(x =>x.Select(z=>z[0]).FirstOrDefault()+"*"+string.Join(",",x.Select(y =>y[1]))).ToList();

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Tuesday, December 5, 2017 6:03 AM
    Tuesday, December 5, 2017 4:23 AM
    Moderator
  • Thank you so much..
    Tuesday, December 5, 2017 6:11 AM
  • Thank you so much..
    Tuesday, December 5, 2017 6:11 AM
  • Hello Uddipto,

    If the question has been solved, you could mark the reply as answer, This can be beneficial to other community members reading this thread. Thanks a lot.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 5, 2017 6:25 AM
    Moderator
  • Thank you so much but this is not generalized solution as the SourceArray may contain more items and there might be repeation and there may be repeation of items.
    so whatever may be the length of the sourceArray the destinationArray would be to insert items correctly.
    if the sourceArray

    SourceArray[0] = "Tom\tis\tcoding";
    SourceArray[1] = "Harry\tin\ttown";
    SourceArray[2] = "Tom\tworking\thard";
    SourceArray[3] = "Harry\tworking\thard";
    SourceArray[4] = "John\tworking\thard";
    
    

    output array would be 

    destinationArray[0]="Tom*is,working";
    destinationArray[1]="Harry*in,working";
    destinationArray[2]="John*working"

    else if the sourceArray can be 

    SourceArray[0] = "Tom\tis\tcoding";

    Then it would able to adjust accordingly

    destinationArray[0]="Tom*is,";

    else if sourceArray contains 

    SourceArray[0]="Tom\tis\tcoding";
    SourceArray[1]="Harry\tin\ttown";
    SourceArray[1]="Tom\tworking\thard";
    Output destination would be 
    destinationArray[0]="Tom*is,working";
    destinationArray[1]="Harry*in";



    • Edited by Uddipto Banerji Tuesday, December 5, 2017 7:46 AM Not Generalized solution
    Tuesday, December 5, 2017 7:38 AM
  • Hello Uddipto,

    The below linq example is suitable for the generalized situations. Thanks for reminding.

      var result = SourceArray.Select(x => x.Split('\t')).GroupBy(y => y[0]).Select(j => j.Key + "*" + string.Join(",", j.Select(k => k[1]))).ToList();

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 6, 2017 7:48 AM
    Moderator
  • Hello Uddipto,

    Is there any update or any other assistance I could provide ? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and patience!

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, December 24, 2017 7:35 AM
    Moderator