removeint n T item remove n occurrences of the specified it

remove(int n, T item) - remove n occurrences of the specified item

removeAllExcept(int n, T item) - remove all but n occurrences of the specified item-This means that only occurrences of the specified item are to be removed

removeHalf(T item) - remove half of the occurrences of the specified item - If the number of occurrences (n) of the item is odd, remove (n-1)/2 occurrences

Write efficient code for each of these operations using an array implementation (JAVA)

Efficient code will go through the array at most a constant number of times, preferably only once. Note the the number of traversals of the array includes those from calls to getFrequencyOf.

Write efficient code for one of these operations using a linked-list implementation

Efficient code will go through the linked list at most a constant number of times, preferably only once. Note the the number of traversals of the linked list includes those from calls to getFrequencyOf.

Solution

hope this will help--

1) Example of Remove -

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
    
   // create an empty array list with an initial capacity
   ArrayList<Integer> arrlist = new ArrayList<Integer>(5);

   // use add() method to add elements in the deque
   arrlist.add(20);
   arrlist.add(15);
   arrlist.add(30);
   arrlist.add(45);

   System.out.println(\"Size of list: \" + arrlist.size());
  
   // let us print all the elements available in list
   for (Integer number : arrlist) {
   System.out.println(\"Number = \" + number);
   }
  
   // Removes element at 3rd position
   arrlist.remove(2);

   System.out.println(\"Now, Size of list: \" + arrlist.size());
  
   // let us print all the elements available in list
   for (Integer number : arrlist) {
   System.out.println(\"Number = \" + number);
   }
   }
}

2) Example of RemoveAll-

List<String> list = new ArrayList<>();
Collections.addAll(list, array);
list.removeAll(Arrays.asList(\"a\"));
array = list.toArray(EMPTY_STRING_ARRAY)

4) Java Program using Linked List

class LinkedList {

    static Node head;

    static class Node {

        int data;
        Node next;

        Node(int d) {
            data = d;
            next = null;
        }
    }

    /* Function to reverse the linked list */
    Node reverse(Node node) {
        Node prev = null;
        Node current = node;
        Node next = null;
        while (current != null) {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        node = prev;
        return node;
    }

    // prints content of double linked list
    void printList(Node node) {
        while (node != null) {
            System.out.print(node.data + \" \");
            node = node.next;
        }
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.head = new Node(85);
        list.head.next = new Node(15);
        list.head.next.next = new Node(4);
        list.head.next.next.next = new Node(20);
       
        System.out.println(\"Original Linked list is :\");
        list.printList(head);
        head = list.reverse(head);
        System.out.println(\"\");
        System.out.println(\"Reversed linked list : \");
        list.printList(head);
    }

remove(int n, T item) - remove n occurrences of the specified item removeAllExcept(int n, T item) - remove all but n occurrences of the specified item-This mean
remove(int n, T item) - remove n occurrences of the specified item removeAllExcept(int n, T item) - remove all but n occurrences of the specified item-This mean

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site