0

New to arrays and its hard to continue also I am not allowed to use LinQ, no Array.Reverse, Array.Sort. I have this as a beginning but I don't know how to continue in order to get only the unique words from the text.

input
pineapple pear citron banana pineapple peach peach kiwi mandarin pineapple cherry pear pear citron

output
banana cherry citron kiwi mandarin peach pear pineapple

string[] array = new string[50];

for (int i = 0; i < array.Length; i++)
{
    array[i] = Console.ReadLine();
    if (array[i] == "")
        break;
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • 2
    `HashSet` allowed? – Andriy Shevchenko Nov 15 '21 at 09:51
  • 1
    Loop through the array and add these fields individually to a new array. Before you add the individual fields you check if it already exists within your new array. When you looped through the first array, you return the new one. – Jelle Nov 15 '21 at 09:53
  • This question has been answered her [How do I remove duplicates from a C# array?](https://stackoverflow.com/questions/9673/how-do-i-remove-duplicates-from-a-c-sharp-array) – Ibram Reda Nov 15 '21 at 09:56
  • @Ibram Reda, No **Linq** allowed! – Auditive Nov 15 '21 at 09:57
  • For extra marks, and a good question for your lecturer/teacher, have you considered that different unicode strings could be rendered as the same word? – Jodrell Nov 15 '21 at 10:00

3 Answers3

0

If it is ok to return IEnumerable

public IEnumerable<T> GetUnique<T>(IEnumerable<T> input) => new HashSet<T>(input);

To return an array with unique strings (without linq).

public string[] GetUnique(string[] input)
{
    var set = new HashSet<string>(input);
    var arr = new string[set.Count];
    var i = 0;
    foreach (var s in set)
        arr[i++] = s;

    return arr;
}
Magnus
  • 45,362
  • 8
  • 80
  • 118
  • If there would be `null` string in source array - it would be included (only once, but would). Not sure about OP expect for nulls at result. – Auditive Nov 15 '21 at 10:29
0

does this help?

public static bool IsInArray(string[] arr,string word){
    for (int i = 0; i < arr.Length; i++)
    {
        if (arr[i] == word)
            return true;
        
    }
    return false;
}

then in the main:

for (int i = 0; i < array.Length; i++)
{
    
    var word = Console.ReadLine();
    if (word == "")
        break;
    if(!IsInArray(array,word)){
        array[i] = word;
    }
    
}
fmansour
  • 555
  • 3
  • 17
0

You can use Array.Find to check whether item in array already exists:

private string[] Distinct(string[] sourceArray)
{
    // Declare target array for unique values
    string[] distinctedArray = new string[sourceArray.Length];

    for (int i = 0; i < sourceArray.Length; i++)
    {
        // Try find element by default equality ==
        if (Array.Find(distinctedArray, x => x == sourceArray[i]) is null)
        {
            // If no item was wound - then add it to result array
            distinctedArray[i] = sourceArray[i];
        }
    }

    // Remove empty elements by joining items into one string and splitting back to array
    distinctedArray = string.Join("\n", distinctedArray)
                            .Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
    return distinctedArray;
}

Sample usage:

void Main(string[] args = default)
{
    // Source array of 14 items
    string[] source = new string[]
    {
        "pineapple",
        "pear",
        "citron",
        "banana",
        "pineapple",
        "peach",
        "peach",
        "kiwi",
        "mandarin",
        "pineapple",
        "cherry",
        "pear",
        "pear",
        "citron"
    };

    string[] distinctedArray = Distinct(source);

    // Result array of 8 items:
    // "pineapple"
    // "pear"
    // "citron"
    // "banana"
    // "peach"
    // "kiwi"
    // "mandarin"
    // "cherry"
}

Default equality comparer == may be replaced with string.Equals method to be able to use StringComparison enum. For example, if you want to care about duplicates like "pear"/"PeAr"/"pEAR", you can specify StringComparison.OrdinalIgnoreCase (I also used .Trim() and ? null-checks):

private string[] Distinct(string[] sourceArray, StringComparison comparisonMode = StringComparison.Ordinal)
{
    string[] distinctedArray = new string[sourceArray.Length];

    for (int i = 0; i < sourceArray.Length; i++)
        if (Array.Find(distinctedArray, x => string.Equals(x?.Trim(), sourceArray[i]?.Trim(), comparisonMode)) is null)
            distinctedArray[i] = sourceArray[i];

    return string.Join("\n", distinctedArray)
                 .Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
}

Example:

void Main(string[] args = default)
{
    string[] source = new string[]
    {
        "pear",
        "Pear",
        "pEAR",
        "pEaR"
    };

    string[] distinctedArray1 = Distinct(source, StringComparison.OrdinalIgnoreCase);
    // Result:
    // "pear"
    string[] distinctedArray2 = Distinct(source, StringComparison.Ordinal);
    // Result:
    // "pear"
    // "Pear"
    // "pEAR"
    // "pEaR"
}
Auditive
  • 1,607
  • 1
  • 7
  • 13