Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.22/5 (3 votes)
See more:
if string S1 = "1,2,3,4";
string S2= "2,3,5,9,10,";



expection result is

string result = 1,4,5,9,10

What I have tried:

please help

here 2 and 3 is common.I want to remove this to result.
Posted
Updated 23-Oct-18 6:59am

An alternative to John's suggestion is to use Linq methods:
string S1 = "1,2,3,4";
string S2 = "2,3,5,9,10,";
string[] p1 = S1.Split(',');
string[] p2 = S2.Split(',');
string result = string.Join(",", p1.Concat(p2).Except(p1.Intersect(p2)));
 
Share this answer
 
Comments
Eric Lynch 23-Oct-18 13:05pm    
Dang it, you beat me to it, so I upvoted your answer.

I was busy over-engineering, for stuff the OP probably doesn't care about...but I was having fun.

With your solution, the ascending numeric order would not be preserved. Also, there's the matter of the bad input data. I'm guessing the trailing comma (on S2) was unintentional, but you never know.

So, just in case, I posted the long-winded version. I might have tied you, but then I went back and added comments :)
OriginalGriff 23-Oct-18 13:59pm    
Yours got my upvote as well!
Member 14024377 23-Oct-18 13:21pm    
thank you very much....

Its working smooothly
OriginalGriff 23-Oct-18 13:57pm    
You're welcome!
OriginalGriff beat me too it, but posting mine anyhow, since it solves a couple of other issues. It preserves ascending order and deals with the trailing comma you have in one of your strings.

using System;
using System.Collections.Generic;
using System.Linq;

namespace DemoApp
{
	public class Program
	{
		public static void Main(string[] args)
		{
			string string1 = "1,2,3,4";
			string string2 = "2,3,5,9,10,";

			// Convert strings to integer arrays to facilitate later sorting
			int[] set1 = GetIntegers(string1);
			int[] set2 = GetIntegers(string2);

			// Get all of the numbers that appear in either set1 or set2
			IEnumerable<int> union = set1.Union(set2);

			// Get all of the numbers that appear in both set1 and set2
			IEnumerable<int> intersection = set1.Intersect(set2);

			// Get the list of numbers we want (union minus intersection) in ascending order
			IEnumerable<int> unionExceptIntersection = union
				.Except(intersection)
				.OrderBy(value => value);

			// Join the numbers back into a string
			string result = string.Join(',', unionExceptIntersection);

			Console.WriteLine(result);
		}

		private static int[] GetIntegers(string text) => text
			// Split values around commas
			.Split(',')
			// Convert string values to integer values (or null)
			.Select(strValue => int.TryParse(strValue, out int intValue) ? intValue : (int?)null)
			// Eliminate invalid integer values
			.Where(value => value.HasValue)
			// Convert to non-nullable integer value
			.Select(value => value.Value)
			// Convert the sequence to an array (so we can iterate more than once)
			.ToArray();
	}
}
 
Share this answer
 
v2
0) Split the strings on comma delimiters.

1) Add each array's element (from the Split statement) to a HashSet object (a HashSet will not allow duplicate element values).

2) Output the contents of the HashSet object.

C#
string[] parts1 = string1.Split(',', false);
string[] parts2 = string2.Split(',', false);

HashSet<string> hash = new HashSet<string>();

foreach(string part in parts1)
{
    hash.Add(part);
}

foreach(string part in parts2)
{
    hash.Add(part);
}

foreach(string part in hash)
{
    Console.WriteLine(part);
}
Console.ReadKey();


The benefit of using a HashSet object is that it will not throw an exception if you try to add a duplicate value. Instead, it simply won't add it. A HashSet is not guaranteed to be in any order, so I leave it as an exercise for the programmer to return the values in the desired sequence.
 
Share this answer
 
Comments
Member 14024377 23-Oct-18 12:41pm    
the above code is not removing common number from 2 string

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900