1

Let's Assume I have Multi dimensional Object array that represents employee information for example

String[,] Employee= new object[,] { 
             {"john","MBA#MBA#BE#MBA#BE"},
             {"jan","MS#MSC#MBA"},

};

Visualize

 john                 MBA#MBA#BE#MBA#BE    
 jan                   MS#MSC#MBA

I want remove the duplicated data ( where data is divided by #)

 john                 MBA#BE   
 jan                  MS#MSC#MBA
2
  • 1
    Why don't you create a proper class instead of using arrays? Commented Nov 4, 2015 at 17:14
  • You'd better use a dictionary for this. Commented Nov 4, 2015 at 17:14

2 Answers 2

1

For this scenario it's better to use a dictionary:

Dictionary<string, string> employee = new Dictionary<string, string>()
{
    {"john", "MBA#MBA#BE#MBA#BE"},
    {"jan", "MS#MSC#MBA"}
}

Dictionary<string, string> result = new Dictionary<string, string>();

foreach (var kvp in employee)
{
    result[kvp.Key] = string.Join("#", kvp.Value.Split('#').Distinct());
}

The result dictionary will contain the result:

// Prints "MS#MSC#MBA"
Console.WriteLine(result["jan"]);

// Prints "MBA#BE"
Console.WriteLine(result["john"]);
Sign up to request clarification or add additional context in comments.

Comments

0

Personally I'd create a class to hold that information

public class Employee
{
    public Employee(string name)
    {
        Name = name;
        Degress = new HashSet<string>();
    }

    public string Name { get; private set; }
    public HashSet<string> Degrees { get; private set; }
}

Then you can populate a list of them like this

string[,] employeeArray = { { "john", "MBA#MBA#BE#MBA#BE" }, { "jan", "MS#MSC#MBA" } };

List<Employee> employees = new List<Employee>();

for (int i = 0; i < employeeArray.GetLength(0); i++)
{
    Employee employee = new Employee(employeeArray[i,0]);
    foreach (var degree in employeeArray[i, 1].Split('#'))
    {
        employee.Degrees.Add(degree);
    }

    employees.Add(employee);
}

foreach (var e in employees)
{
    Console.WriteLine(e.Name + " " + string.Join(" ", e.Degress));
}

Which prints

john MBA BE

jan MS MSC MBA

Because Degrees is a HashSet it will not allow duplicates, and when you attempt to pass a duplicate to Add it will just return false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.