Why does SortedSet Contains function returns false for the first element of the set itself?

ANOOP SINGH 0 Reputation points
2023-05-07T19:14:50.3366667+00:00

Hi Team,

I was working with the SortedSet Data Structure and surprisingly I ended up finding an issue with the implementation.

When I tried to remove the first element from the SortedSet, It did not remove the element and while debugging I found Sorted Set returns false for the element which it returns as a result of First() method call.

Sample code:


public class Solution {
    public int LeastInterval(char[] tasks, int n) {
        if(tasks == null || tasks.Length == 0)
            return 0;
		// map of tasks with corresponding count.
        Dictionary<char, int> map = new Dictionary<char, int>();
        foreach(char task in tasks)
        {
            if(map.ContainsKey(task))
                map[task]++;
            else
                map.Add(task, 1);
        }

		// Adding the tasks to SortedSet and sorting based on the task count in Dictionary.
        SortedSet<char> uniqueTasks = new SortedSet<char>(Comparer<char>.Create((a,b) => map[b] > map[a] ? -1 : 1));

        foreach(var c in map)
        {
            uniqueTasks.Add(c.Key);
        }

        int intervals = 0;
        while(uniqueTasks.Count != 0)
        {
            IList<char> currentTaskList = new List<char>();
            for(int i = 0; i <= n; i++)
            {
                if(uniqueTasks.Count == 0)
                    break;

				// Here I get the first task
                char currentTask = uniqueTasks.First();

				// It is returning false, How can this be false????????
                Console.WriteLine(uniqueTasks.Contains(uniqueTasks.First()));
                Console.WriteLine(currentTask);
                map[currentTask]--;
                currentTaskList.Add(currentTask);
            }


            foreach(char t in currentTaskList)
            {
                if(map[t] > 0)
                    uniqueTasks.Add(t);
            }

            if(uniqueTasks.Count == 0)
                intervals += n+1;
            else
                intervals += currentTaskList.Count;
        }

        return intervals;
    }
}

/*

Sample Input 
tasks = ["A","A","A","B","B","B"]
n = 2
*/
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2023-05-07T19:26:45.2266667+00:00

    Use a valid comparer. For example:

    SortedSet<char> uniqueTasks = new SortedSet<char>( Comparer<char>.Create( ( a, b ) => a.CompareTo( b ) ) );
    // or, if this has sense:
    SortedSet<char> uniqueTasks = new SortedSet<char>( Comparer<char>.Create( ( a, b ) => map[a].CompareTo( map[b] ) ) );
    

    However, the latter seems problematic if you change the values of map.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.