Question

In: Computer Science

(JAVA) Repeated Class diagram: Repeated + repeatedDetector(ArrayList<Integer> a) : int For this exercise, you will have...

(JAVA)

Repeated

Class diagram:

Repeated

+ repeatedDetector(ArrayList<Integer> a) : int

For this exercise, you will have to implement the class diagram above. Basically, the objective of this exercise is to develop the class and the previous method, in such a way that it tells us the amount of "repeated" elements that are in the dynamic array.

When we speak of "repeated", we mean counting the number of numbers of the same value that are found in succession.

Example 1: The method receives a dynamic array ArrayList <Integer>, which internally has this data: [1,1,4,1,4,1,1]

The method returns 2 (since it found 2 sequences of equal numbers).

Example 2: the method receives a dynamic array ArrayList <Integer>, which internally has this data: [1,1,1,1,1]

The method returns 1 (since it found 1 sequence of equal numbers).

Solutions

Expert Solution

import java.util.*;
class repeat
{
    public int repeatDectetor(ArrayList<Integer>a)
    {
        
        int c=0;
        Map<Integer, Integer> duplicates = new HashMap<Integer, Integer>(); 
 
        for (int i : a) { //maping the duplicate item in hash table
           if (duplicates.containsKey(i)) { 
              duplicates.put(i, duplicates.get(i) + 1); 
           } else { 
              duplicates.put(i, 1); 
           } 
        } 

        for (Map.Entry<Integer, Integer> entry : duplicates.entrySet()) 
        { 
            if(entry.getValue()>1)//counting the duplicate items
                c++; 
         } 
   
        return c;
    }
    public static void main(String[] args) {
        ArrayList<Integer> list=new ArrayList<Integer>();
        repeat obj=new repeat();
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(4);
        list.add(1);
        list.add(4);
        list.add(1);
        list.add(1);
        

        int x=obj.repeatDectetor(list);
        System.out.print("number of duplicate element is "+x);
    }
}

output:

number of duplicate element is 2


Related Solutions

(JAVA) Balanced Class diagram: Balanced + swing(int[] a) : boolean For this exercise, you will have...
(JAVA) Balanced Class diagram: Balanced + swing(int[] a) : boolean For this exercise, you will have to implement the class diagram above. Basically, the objective of this exercise is to develop the class and the previous method, in such a way that it tells us if an array is balanced or not. We say that an array is balanced if the sum of the elements belonging to the first half of the array (not including the element in the middle),...
JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm =...
JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm = new HashMap<Integer,Integer>(); for (int i= 0; i<2; i++) { al.add(i); al.add(i+1); hs.add(i); hs.add(i+1); hm.put(al.get(i), al.get(i+1)); hm.put(hm.get(i), hm.get(i+1)); }   System.out.println(al); System.out.println(hs); System.out.println(hm); // {key=value}. ---------------------------------- What output is produced by the following code and why?
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c]...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c] , [d,e], [f] ] ----> [ [a,d,f], [a,e,f], [b,d,f], [b,e,f], [c,d,f], [c,e,f]] [ [a,b], [a,b,c]] ----->[[a,a],[a,b],[a,c], [b,a],[b,b],[b,c] assuming abc are integer Thanks in advance!
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist to store/ add product in stock 3. Product must have name, code number and quantity in stock 4. Print list of the products and their stock quantity 5. Search products based on their name 6. Remove product based on their code number 7. Sell product and deduct quantity from stock 8. Stock Class contains methods of search product, add product and check quantity of...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist...
Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist to store/ add product in stock 3. Product must have name, code number and quantity in stock 4. Print list of the products and their stock quantity 5. Search products based on their name 6. Remove product based on their code number 7. Sell product and deduct quantity from stock 8. Stock Class contains methods of search product, add product and check quantity of...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and returns the expanded ArrayList.  The total size will be (k+1) * n.   public ArrayList<Integer> makeCopies(ArrayList<Integer>, int k) { } Example: ArrayList<Integer> has (3,7,4) and k = 2, then the returned, expanded ArrayList will have (3,7,4,3,7,4,3,7,4).  The total size is (k+1)*n = (2+1)*3= 9.
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT