Showing posts with label hackerrank. Show all posts
Showing posts with label hackerrank. Show all posts

Saturday, February 22, 2020

how to remove duplicate records in the linked list?

the hackerrank post today challenge is to remove duplicate record from integer linklist.

i use hashset to store the unique integer from the linklist, if the current node data already exist in the hashset. i simply pointing the previous node to the current node next pointer. if not found, i add the current node data to the hashset and move to the next node.


here is the sample code in C#

using System;
using System.Collections.Generic;
class Node
{
public int data;
public Node next;
    public Node(int d){
        data=d;
        next=null;
    }
}
class Solution {
  public static Node removeDuplicates(Node head){
    //Write your code here
    if (head==null){
        return null;
    }
    HashSet<int> hashset =new HashSet<int>();
    Node prevNode = head;
    Node currentNode =prevNode.next;
    if(currentNode==null){
        return head;
    }
    hashset.Add(prevNode.data);
    while(currentNode!=null)
        {
        if(hashset.Contains(currentNode.data))
            {
            prevNode.next=currentNode.next;
            currentNode=currentNode.next;
        }
        else{
            hashset.Add(currentNode.data);
              prevNode=currentNode;
            currentNode=currentNode.next;
        }
    }
    return head;
  }
public static  Node insert(Node head,int data)
{
        Node p=new Node(data);
if(head==null)
head=p;
else if(head.next==null)
head.next=p;
else
{
Node start=head;
while(start.next!=null)
start=start.next;
start.next=p;
}
return head;
    }
public static void display(Node head)
{
Node start=head;
while(start!=null)
{
Console.Write(start.data+" ");
start=start.next;
}
}
    static void Main(String[] args) {
Node head=null;
        int T=Int32.Parse(Console.ReadLine());
        while(T-->0){
            int data=Int32.Parse(Console.ReadLine());
            head=insert(head,data);
        }
      head=removeDuplicates(head);
display(head);
}
}

Friday, February 21, 2020

how to use Queue to implement a binary searh true Level Order algorithm?

Currently i had joined the Hackerrank challenag for 30 days

today i got a very interesting topic to use Queue to implement the level Order function

using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; class Node{ public Node left,right; public int data; public Node(int data){ this.data=data; left=right=null; } } class Solution{ static void levelOrder(Node root){ Queue<Node> nodeQueue=new Queue<Node>(); if(root !=null){ nodeQueue.Enqueue(root); } while(nodeQueue.Count>0){ Node current=nodeQueue.Dequeue(); Console.Write($"{current.data} "); if(current.left !=null){ nodeQueue.Enqueue(current.left); } if(current.right !=null){ nodeQueue.Enqueue(current.right); } } } static Node insert(Node root, int data){ if(root==null){ return new Node(data); } else{ Node cur; if(data<=root.data){ cur=insert(root.left,data); root.left=cur; } else{ cur=insert(root.right,data); root.right=cur; } return root; } } static void Main(String[] args){ Node root=null; int T=Int32.Parse(Console.ReadLine()); while(T-->0){ int data=Int32.Parse(Console.ReadLine()); root=insert(root,data); } levelOrder(root); } }

Thursday, February 6, 2020

A simple and stupid PhoneBook in C#

I just completed a HackerRank Challenge today. the topic is about to write a program for a phonebook which allow the user to input contact information and support searching contact by name.

initial i finished it only support number of input search.Finally i have to use the while to support unlimited number of search..


here is the snippet of code. i hope you will enjoy it

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT.
Your class should be named Solution */
Dictionary<string, string> phoneBook=new Dictionary<string, string>();
int totalContact=Int32.Parse(Console.ReadLine());
for(var i=0; i<totalContact; i++){
string input=Console.ReadLine();
string[] strArray=input.Split(null);
if(strArray.Length>=2){
phoneBook.Add(strArray[0], strArray[1]);
}
}
//use the while loop to support unspecific number of search
while(true){
try{
string key=Console.ReadLine();
if (phoneBook.ContainsKey(key))
{
Console.WriteLine($"{key}={phoneBook[key]}");
}
else
{
Console.WriteLine("Not found");
}
}catch(System.Exception ex){
break;
}
}
}
}

cheer