Get all unique values out of strings












0















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?










share|improve this question

























  • What did you mean by unique value ?

    – Saif
    Nov 22 '18 at 6:34






  • 2





    do some research on linq and distinct

    – JohnB
    Nov 22 '18 at 6:35











  • He probably means distinct values.

    – Uwe Keim
    Nov 22 '18 at 6:35











  • No I guess OP want value where occurrence is one

    – Prasad Telkikar
    Nov 22 '18 at 6:37











  • OP wants to get a distinct list of numbers i believe - ie no duplicates

    – JohnB
    Nov 22 '18 at 6:38


















0















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?










share|improve this question

























  • What did you mean by unique value ?

    – Saif
    Nov 22 '18 at 6:34






  • 2





    do some research on linq and distinct

    – JohnB
    Nov 22 '18 at 6:35











  • He probably means distinct values.

    – Uwe Keim
    Nov 22 '18 at 6:35











  • No I guess OP want value where occurrence is one

    – Prasad Telkikar
    Nov 22 '18 at 6:37











  • OP wants to get a distinct list of numbers i believe - ie no duplicates

    – JohnB
    Nov 22 '18 at 6:38
















0












0








0








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?










share|improve this question
















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?







c# .net string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:49









Dmitry Bychenko

112k1099141




112k1099141










asked Nov 22 '18 at 6:31









EdgarsEdgars

1




1













  • What did you mean by unique value ?

    – Saif
    Nov 22 '18 at 6:34






  • 2





    do some research on linq and distinct

    – JohnB
    Nov 22 '18 at 6:35











  • He probably means distinct values.

    – Uwe Keim
    Nov 22 '18 at 6:35











  • No I guess OP want value where occurrence is one

    – Prasad Telkikar
    Nov 22 '18 at 6:37











  • OP wants to get a distinct list of numbers i believe - ie no duplicates

    – JohnB
    Nov 22 '18 at 6:38





















  • What did you mean by unique value ?

    – Saif
    Nov 22 '18 at 6:34






  • 2





    do some research on linq and distinct

    – JohnB
    Nov 22 '18 at 6:35











  • He probably means distinct values.

    – Uwe Keim
    Nov 22 '18 at 6:35











  • No I guess OP want value where occurrence is one

    – Prasad Telkikar
    Nov 22 '18 at 6:37











  • OP wants to get a distinct list of numbers i believe - ie no duplicates

    – JohnB
    Nov 22 '18 at 6:38



















What did you mean by unique value ?

– Saif
Nov 22 '18 at 6:34





What did you mean by unique value ?

– Saif
Nov 22 '18 at 6:34




2




2





do some research on linq and distinct

– JohnB
Nov 22 '18 at 6:35





do some research on linq and distinct

– JohnB
Nov 22 '18 at 6:35













He probably means distinct values.

– Uwe Keim
Nov 22 '18 at 6:35





He probably means distinct values.

– Uwe Keim
Nov 22 '18 at 6:35













No I guess OP want value where occurrence is one

– Prasad Telkikar
Nov 22 '18 at 6:37





No I guess OP want value where occurrence is one

– Prasad Telkikar
Nov 22 '18 at 6:37













OP wants to get a distinct list of numbers i believe - ie no duplicates

– JohnB
Nov 22 '18 at 6:38







OP wants to get a distinct list of numbers i believe - ie no duplicates

– JohnB
Nov 22 '18 at 6:38














6 Answers
6






active

oldest

votes


















3














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.







share|improve this answer


























  • and if you're OCD, OrderBy...

    – JohnB
    Nov 22 '18 at 6:40











  • @JohnB OCD indeed :)

    – Michael Randall
    Nov 22 '18 at 6:44



















3














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}





share|improve this answer


























  • I like this approach upvote, also i need to use my upvotes for the day

    – Michael Randall
    Nov 22 '18 at 7:03













  • Last one is awesome,+1

    – Prasad Telkikar
    Nov 22 '18 at 7:06





















0















  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






share|improve this answer

































    0














    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






    share|improve this answer































      0














      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






      share|improve this answer































        0














        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();





        share|improve this answer
























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425077%2fget-all-unique-values-out-of-strings%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          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.







          share|improve this answer


























          • and if you're OCD, OrderBy...

            – JohnB
            Nov 22 '18 at 6:40











          • @JohnB OCD indeed :)

            – Michael Randall
            Nov 22 '18 at 6:44
















          3














          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.







          share|improve this answer


























          • and if you're OCD, OrderBy...

            – JohnB
            Nov 22 '18 at 6:40











          • @JohnB OCD indeed :)

            – Michael Randall
            Nov 22 '18 at 6:44














          3












          3








          3







          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.







          share|improve this answer















          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.








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 6:43

























          answered Nov 22 '18 at 6:39









          Michael RandallMichael Randall

          36.8k84069




          36.8k84069













          • and if you're OCD, OrderBy...

            – JohnB
            Nov 22 '18 at 6:40











          • @JohnB OCD indeed :)

            – Michael Randall
            Nov 22 '18 at 6:44



















          • and if you're OCD, OrderBy...

            – JohnB
            Nov 22 '18 at 6:40











          • @JohnB OCD indeed :)

            – Michael Randall
            Nov 22 '18 at 6:44

















          and if you're OCD, OrderBy...

          – JohnB
          Nov 22 '18 at 6:40





          and if you're OCD, OrderBy...

          – JohnB
          Nov 22 '18 at 6:40













          @JohnB OCD indeed :)

          – Michael Randall
          Nov 22 '18 at 6:44





          @JohnB OCD indeed :)

          – Michael Randall
          Nov 22 '18 at 6:44













          3














          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}





          share|improve this answer


























          • I like this approach upvote, also i need to use my upvotes for the day

            – Michael Randall
            Nov 22 '18 at 7:03













          • Last one is awesome,+1

            – Prasad Telkikar
            Nov 22 '18 at 7:06


















          3














          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}





          share|improve this answer


























          • I like this approach upvote, also i need to use my upvotes for the day

            – Michael Randall
            Nov 22 '18 at 7:03













          • Last one is awesome,+1

            – Prasad Telkikar
            Nov 22 '18 at 7:06
















          3












          3








          3







          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}





          share|improve this answer















          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}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 7:01

























          answered Nov 22 '18 at 6:55









          Dmitry BychenkoDmitry Bychenko

          112k1099141




          112k1099141













          • I like this approach upvote, also i need to use my upvotes for the day

            – Michael Randall
            Nov 22 '18 at 7:03













          • Last one is awesome,+1

            – Prasad Telkikar
            Nov 22 '18 at 7:06





















          • I like this approach upvote, also i need to use my upvotes for the day

            – Michael Randall
            Nov 22 '18 at 7:03













          • Last one is awesome,+1

            – Prasad Telkikar
            Nov 22 '18 at 7:06



















          I like this approach upvote, also i need to use my upvotes for the day

          – Michael Randall
          Nov 22 '18 at 7:03







          I like this approach upvote, also i need to use my upvotes for the day

          – Michael Randall
          Nov 22 '18 at 7:03















          Last one is awesome,+1

          – Prasad Telkikar
          Nov 22 '18 at 7:06







          Last one is awesome,+1

          – Prasad Telkikar
          Nov 22 '18 at 7:06













          0















          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






          share|improve this answer






























            0















            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






            share|improve this answer




























              0












              0








              0








              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






              share|improve this answer
















              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







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 22 '18 at 6:56

























              answered Nov 22 '18 at 6:39









              Prasad TelkikarPrasad Telkikar

              2,260522




              2,260522























                  0














                  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






                  share|improve this answer




























                    0














                    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






                    share|improve this answer


























                      0












                      0








                      0







                      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






                      share|improve this answer













                      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







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 22 '18 at 7:12









                      SaifSaif

                      1,1741825




                      1,1741825























                          0














                          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






                          share|improve this answer




























                            0














                            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






                            share|improve this answer


























                              0












                              0








                              0







                              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






                              share|improve this answer













                              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







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 22 '18 at 7:37









                              Rod lauro RomarateRod lauro Romarate

                              12




                              12























                                  0














                                  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();





                                  share|improve this answer




























                                    0














                                    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();





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      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();





                                      share|improve this answer













                                      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();






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 22 '18 at 8:23









                                      Code ChapterCode Chapter

                                      86




                                      86






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425077%2fget-all-unique-values-out-of-strings%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                                          ComboBox Display Member on multiple fields

                                          Is it possible to collect Nectar points via Trainline?