Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to check duplicated string in list

Discussion in 'Scripting' started by Dzxyan, Jan 23, 2015.

  1. Dzxyan

    Dzxyan

    Joined:
    Sep 23, 2013
    Posts:
    167
    as example

    string[] a = "a,b,c,d,a,a,c,e,f, b,b, b";

    how to check duplicated and show if have duplicated like this
    a 3
    b 4
    c 2

    thanks for everyone
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Ensure you are using System.Collections.Generic;

    Code (CSharp):
    1. Dictionary<string, int> CountInstancesOfString (string[] arr) {
    2.     var result = new Dictionary<string, int>();
    3.  
    4.     foreach(var str in arr) {
    5.      
    6.         if (result.ContainsKey(str))
    7.             result[str]++;
    8.         else
    9.             result.Add(str, 0);      
    10.  
    11.     }
    12.  
    13.     return result;
    14. }
     
  3. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
    If you put that in C# that will just be one string. An array of strings would be more like

    Code (CSharp):
    1. string[] stringarray = { "aa", "bb", "cc", "dd" };
    For a list it would be more like

    Code (CSharp):
    1. List<string> listStrings = new List<string>();
    2.  
    3. lstStrings.Add("aa");
    4. lstStrings.Add("bb");
    5. lstStrings.Add("cc");
    6. lstStrings.Add("dd");
    In other case you can run a for loop to iterate through the whole thing. Foreach can also work. How you do the counting depends on exactly what you want to do and what you mean by the same. It also depends on what you intend on doing with that information once you get it.
     
  4. Dzxyan

    Dzxyan

    Joined:
    Sep 23, 2013
    Posts:
    167
    I'm a bit confused
    Maybe i'm not so familiar with this
    can you explain more details?
     
  5. Dzxyan

    Dzxyan

    Joined:
    Sep 23, 2013
    Posts:
    167
    the string array is random generated
    the array list and array string is ramdomly
     
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
  7. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Well, it's pretty straight forward. This method takes an array of strings.
    Code (CSharp):
    1.  
    2. //so, arr would be {"aa","bb","aa","bb","aa"}, whatever.
    3. Dictionary<string, int> CountInstancesOfString (string[] arr) {
    4.  
    5.     //This is a dictionary that uses strings as keys and ints as values
    6.     var result = new Dictionary<string, int>();
    7.  
    8.      //We loop through every item in the array.
    9.     foreach(var str in arr) {
    10.    
    11.         //If the dictionary contains the string, it increments the value by one
    12.         if (result.ContainsKey(str))
    13.             result[str]++;
    14.         else
    15.            //Otherwise it creates it.
    16.             result.Add(str, 1);    
    17.  
    18.     }
    19.  
    20.     //So, as in the above example, the result dictionary would hold:
    21.     //"aa" : 3
    22.     //"bb" : 2
    23.     return result;
    24. }