Question
Data structures question
(e 7 (9) Modify the pool data type in the following way: Assume now that every element can appear at most once in the data structure. An insertion must now check for duplicates Implement the same operations as before, but with duplicate checking. What is th complexity of each operation in the worst case IEl 10 points)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution: import java.util.*; import java.lang.*i class ModifyMain static boolean checkDuplicatesFunc (int all, int s) // thipublic static void main (Stringl] args) //array elements int al 10, 15, 13, 14, 13, 25, 36 if (checkDuplicatesFunc (a, 13) )Copyable Code:

import java.util.*;

import java.lang.*;

class ModifyMain

{

    static boolean checkDuplicatesFunc(int a[], int s)

    {

        // this is for empty hashset

        HashSet<Integer> hset = new HashSet<Integer>();

        for (int x=0; x<a.length; x++)

        {

            // If the element is already present in hash, then we found

            // a duplicate within s

            if (hset .contains(a[x]))

               return true;

            //insert element to hashset

            hset .add(a[x]);

            // delete the s+1

            if (x >= s)

              hset .remove(a[x-s]);

        }

        return false;

    }

     public static void main (String[] args)

    {

        //array elements

        int a[] = {10, 15, 13, 14, 13, 25, 36};

        if (checkDuplicatesFunc(a, 13))

           System.out.println("The element is duplicate");

        else

           System.out.println("The element is not duplicate");

    }

}

Add a comment
Know the answer?
Add Answer to:
Data structures question Modify the pool data type in the following way: Assume now that every...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Describe the most time-efficient way to implement the operations listed below. Assume no duplicate values and...

    Describe the most time-efficient way to implement the operations listed below. Assume no duplicate values and that you can implement the operation as a member function of the class - with access to the underlying data structure. Then, give the tightest possible upper bound for the worst case running time for each operation in terms of N. (both implemented using an arm elements into a single binary min heap. Explanation:

  • Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regu...

    Data Structure Java code Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regular queue data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to the order in which they were enqueued A typical priority queue supports following...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • Question 2 In this question, you will read two data files that include integers into two...

    Question 2 In this question, you will read two data files that include integers into two different arrays – the same way we did in class (but we are doing to arrays here). Duplicates are ok. 1- After you read the data into the array (use one function that takes an int array and a dsize by reference just like we did in class, and call that from main to fill both arrays). 2- Include a printArray function so that...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • CSC311: For a while now we have been discussing reference-based data structures and we have seen...

    CSC311: For a while now we have been discussing reference-based data structures and we have seen reference-based implementations of the List, Stack, and Queue ADT. For this program, you will write a reference-based implementation of the Dictionary ADT as follows: a. A Dictionary ADT is an (abstract) data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection, promptly. b. Operations associated with this data type allow: a. the...

  • In this project, you will be writing an object that implements an ordered linked list with...

    In this project, you will be writing an object that implements an ordered linked list with operator overload support for insertions and deletions. The specification for the list object will be provided upfront and you must design an implementation that supports the provided specification. In addition, you will be given a list of tasks that should be performed. CIS 221 Programming II C++ Programming Project operator overloaded ordered Linked List object Overview In this assignment, the student will write a...

  • C++ Modify this program (or create your own) so that it performs the following tasks: 1....

    C++ Modify this program (or create your own) so that it performs the following tasks: 1. Insert the numbers into the list in sorted order (ascending). This will require you to keep the list sorted as you continue to insert new numbers. 2. When adding a new number, first check to see whether it is larger than the last number currently in the list. If it is, add it directly to the end of the list, i.e., do not traverse...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT