Please help a C# guy understand a JAVA mystery! (seemingly)

  • 0

    I am looking at this answer

    JAVA mystery? Or I am missing something. I came up with the exact code as in above answer in C# and when I was trying to figure out to remove dups, I succeeded in some cases but failed for few tests. So, I had to look at solutions and found the above answer.

    In this answer, res is a HashSet of list of integers. In the findsequence recursive helper, you are adding to the res set, a new list of whatever you have in the holder. So, when you have duplicate set again, you are going to add another new list.

    My question is, how in the world res is able to figure out that this is a duplicate set when in fact those are two seperate instances of lists?

    It is not possible in C#, not sure how it works in JAVA

  • 0

    This recursive function helped me. I could not make it work by using a global hashset to remove duplicate subsequences. Not sure how it worked in JAVA, but the below worked. Here we are having a local hashSet which goes along the recursion tree

    public void dfs(int[] nums, HashSet<IList<int>> result, IList<int> list, int index) {
            if (list.Count >= 2) {
                result.Add(new List<int>(list));
            var used = new HashSet<int>();
            for (int i = index; i < nums.Length; i++) {
                if (used.Contains(nums[i])) 
                if(list.Count == 0 || nums[i] >= list[list.Count -1]){
                    dfs(nums, result, list, i + 1);
                    list.RemoveAt(list.Count - 1);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.