Home > OS >  delete nodes with values that are repeated more than 2 times
delete nodes with values that are repeated more than 2 times

Time:01-24

I have a method that I want to delete nodes with values that are repeated more than 2 times, unfortunately it delete nodes that are appearing more than once. the input am inputting is

E B E E B A B

but I end up getting

E B A

Instead of getting

E B E B A

What am I doing wrong, have been scratching my head for hours,and I am not seeeing the issue, any guidance towards the right direction is accepted. Thank you in advance?

this is my code

using System;
using System.Collections.Generic;

class removeDuplicates
{
    class node
    {
        public String val;
        public node next;

        public node(String val) { this.val = val; }
    }

    static void removeDuplicate(node head)
    {

        HashSet<String> hs = new HashSet<String>();

        node current = head;
        node prev = null;
        while (current != null)
        {
            String curval = current.val;

        
            if (hs.Contains(curval))
            {
                prev.next = current.next;
            }
            else
            {
                hs.Add(curval);
                prev = current;
            }
            current = current.next;
        }
    }
    
    static void printList(node head)
    {
        while (head != null)
        {
            Console.Write(head.val   " ");
            head = head.next;
        }
    }

    // Driver code
    public static void Main(String[] args)
    {

        // The constructed linked list is:
        // E->B->E->E->B->A->B
        node start = new node("E");
        start.next = new node("B");
        start.next.next = new node("E");
        start.next.next.next = new node("E");
        start.next.next.next.next = new node("B");
        start.next.next.next.next.next = new node("A");
        start.next.next.next.next.next.next = new node("B");

        Console.WriteLine("Linked list before removing "
                          "duplicates :");
        printList(start);

        removeDuplicate(start);

        Console.WriteLine("\nLinked list after removing "
                          "duplicates :");
        printList(start);
    }
}

CodePudding user response:

I believe you want a counter here. The previous answer is correct if you want only to remove adjacent duplicates.

static void RemoveDuplicate(node head)
{
    // to keep count
    Dictionary<string, int> dc = new Dictionary<string, int>();

    node current = head;
    node prev = null;

    while (current != null)
    {
        string curval = current.val;

        if (dc.ContainsKey(curval))
        {
            int currentCount = dc[curval];

            if (currentCount >= 2)
            {
                prev.next = current.next;
            }
            else
            {
                prev = current;
            }
            
            // increment count
            dc[curval] = currentCount   1;

        }
        else
        {
            dc.Add(curval, 1);
            prev = current;
        }

        current = current.next;
    }
}

This gives you below:

E B E B A

CodePudding user response:

Now, I've renamed your fields and method names to be in line with standard C# naming conventions.

Here's the method you need to remove duplicates:

static void RemoveDuplicate(Node head)
{
    Node prev = head;
    Node current = prev.Next;
    while (current != null)
    {
        if (prev.Val == current.Val)
        {
            prev.Next = current.Next;
        }
        else
        {
            prev = current;
        }
        current = current.Next;
    }
}

That gives me:

Linked list before removing duplicates :
E B E E B A B 
Linked list after removing duplicates :
E B E B A B 
  •  Tags:  
  • Related