Edgars
Edgars

Reputation: 1

Get all unique values out of strings

I am facing some problems while getting unique values out of strings.

Example:

string1 = "4,5"
string2 = "7,9"
string3 = "4,7,6,1"
string4 = "1"

After I need to get all unique values as an int. In this case result must be 6. But each time the number of strings can change.

Is this even possible?

Upvotes: 0

Views: 3516

Answers (6)

Code Chapter
Code Chapter

Reputation: 37

This is a longer one than the others, but it might be easier to understand how it works.

        List<string> str = new List<string> {"1", "3", "1", "2", "3", "43", "23", "54", "3"," "4 };

        List<string> foundstr = new List<string> { };

        foreach (string check in str)
        {
            bool found = false;
            //going through every single item in the list and checking if it is found in there
            for (int i = 0; i < foundstr.Count; i++)
            {
            //if found then make found(bool) true so we don't put it in the list
                if(check == foundstr[i])
                {
                    found = true;
                }  
            }
            //checking if the string has been found and if not then add to list
            if(found == false)
            {
                foundstr.Add(check);
            }
        }

        foreach(string strings in foundstr)
        {
            Console.WriteLine(strings);
        }

        Console.ReadLine();

Upvotes: 0

Rod lauro Romarate
Rod lauro Romarate

Reputation: 123

He can you try this out

var string1 = "4,5";
var string2 = "7,9";
var string3 = "4,7,6,1";
var string4 = "1";

var allStrings = string1 + ',' + string2 + ',' + string3 + ','+ string4;
var distinctNumbers = new List<string>(allStrings.Split(',').Distinct());

Output : 4 5 7 9 6 1

distinctNumbers = Count = 6

Upvotes: 0

Saif
Saif

Reputation: 2689

A single line can do the job

    string s = "1,3,1,2,3,43,23,54,3,4";
    string[] StrArry = s.Split(',');

    int[] IntArry = Array.ConvertAll(StrArry, int.Parse).Distinct().ToArray();

output

1,3,2,43,23,54,4

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186843

If "number of strings can change", let's organize them into a collection:

List<string> strings = new List<string> {
  "4,5",
  "7,9",  
  "4,7,6,1",
  "1"
};

Then we can right a simple Linq:

var uniques = strings
  .SelectMany(item => item.Split(',')) // split each item and flatten the result
  .Select(item => int.Parse(item))
  .Distinct()
  .ToArray(); // let's have an array of distinct items: {4, 5, 7, 9, 6, 1}

If you want to obtain items which appears just once:

var uniques = strings
  .SelectMany(item => item.Split(',')) // split each item and flatten the result
  .Select(item => int.Parse(item))
  .GroupBy(item => item) 
  .Where(item => item.Count() == 1)
  .Select(group => group.Key)
  .ToArray(); // let's have an array of items which appear once: {5, 9, 6} 

Upvotes: 3

Prasad Telkikar
Prasad Telkikar

Reputation: 16079

  1. Instead of using number of string variable you can you single instance of StringBuilder

  2. Convert all element to array of integer.

  3. Get Distinct/number which comes only one once by Linq

Something like this:

    StringBuilder sb = new StringBuilder();
    sb.Append("5,5");
    sb.Append(",");
    sb.Append("7,9");
    sb.Append(",");
    sb.Append("4,7,6,1");
    sb.Append(",");
    sb.Append("1"); 

    string[] arr = sb.ToString().Split(',');
    int[] test = Array.ConvertAll(arr, s => int.Parse(s));


    var count = test
        .GroupBy(e => e)
        .Where(e => e.Count() == 1)
        .Select(e => e.First()).ToList();

output: 

9
4
6

POC: .netFiddler

Upvotes: 0

TheGeneral
TheGeneral

Reputation: 81583

Use Split and Distinct

var  input = "1,3,1,2,3,43,23,54,3,4";

var result input.Split(',')
                .Distinct();    

Console.WriteLine(string.Join(",",result));

Output

1,3,2,43,23,54,4

Full Demo Here


Additional Resources

String.Split Method

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array.

Enumerable.Distinct Method

Returns distinct elements from a sequence.

Upvotes: 3

Related Questions