Count How Many Duplicates Are In ArrayList?
May 14, 2015
I'm trying to count the number of elements in an ArrayList which also have duplicates. So for example, in an ArrayList of strings which contains cat, cat, dog, horse, zebra, zebra, the answer should be two.
If an element is found to be a duplicate, that element should then be exempt from the search so if that element is found again it should not increase the duplicate count.
Here is my code:
public int countDuplicates() {
int duplicates = 0;
// TODO: Write the code to get the number of duplicates in the list
for (int i = 0; i < list.size()-1;i++) {
boolean found = false;
[Code] ....
I know it's wrong because right now it's still increasing the duplicate count for elements that have already been detected as duplicates. How can I make it stop doing this?
View Replies
ADVERTISEMENT
Apr 12, 2015
I have taken array int[] a = {33,33,5,5,9,8,9,9};
In this array so many values are duplicates means 33 comes twice & 5 also comes twice & 9 comes three times. But I want to count the first value which is duplicate means 33 is first value which comes twice so answer would be 2.
I try:
public class FindFirstDuplicate {
public static void main(String[] args) {
int c=0;
int[] a = {33,33,5,5,9,8,9,9};
outerloop:
for(int i = 0; i < a.length; i++) {
[code]....
Output:
33
Count: 1
View Replies
View Related
Nov 5, 2014
I'm struggling with that piece of code, my intention is to check for the object I want to add before adding it, so there won't be any duplicate on my list. I'm not sure how could I do that, since I'm working with objects.
Person is a class with few parameters such as id, name, and few others.
I guess I should search for a person with the same id, since that has be unique, but can't get it right.
private ArrayList<person> model= new ArrayList<>();
//...
if (model.contains(person))throw new IllegalArgumentException("duplicate");
else model.addElement(person);
View Replies
View Related
Jan 29, 2015
We have been given an assignment to create a small library, with the following classes Library, Book, Author and Copy. With a given class Biblio which has predefined code and adds the books to the class book in an arraylist in Class Copy.
The UML Domain is attached so you know the flow of the classes Everything is working fine and the generated output is correct.
There is just one method in class Library that is not working, the int method has to count the number of Copy's based on the Class Book (String):
I have to go through the Arraylist in Class Copy and look for a specific book and return the number of copy's.
I have tried multiple steps using a for loop Now I have found a similar post the uses hashset, I have tried below code but the return comes back with 0. (There are 3 copy's)
package domein;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import domein.Boek;
import domein.Exemplaar;
[code]....
View Replies
View Related
Sep 11, 2014
What I'm trying to do here is to count the vowels in an arraylist of strings. What I did may not be right, but that's not my problem for now. My problem is that i cannot return the value (n) I want to return. I don't know why.
import java.util.*;
import java.util.Arrays;
public class One {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("aaa");
list.add("brr");
list.add("unn");
System.out.println(vowels(list));
[URL] ....
View Replies
View Related
Jun 30, 2014
I have an array with the following characters {'E', 'L','E','P','H','A','N','T','P','O'}
now, I need an array that will store the first array such that only the occurence occurs e.g {'E','L','P','H','A','N','T','O'} Notice that the characters 'E' and 'P' occur twice and as a result were not repeated the second time in the new array.
How would one go about this using the counting elements technique?
I tried this but not sure how to use the counting elements technique.
char [] arr = new char{'E', 'L','E','P','H','A','N','T','P','O'};
char[] bucket = new char[(arr[0] * arr.length)];
for (int i = 0; i < len; i++)
bucket[arr[i]]++;
View Replies
View Related
Nov 2, 2014
THE PROGRAM DOES NOT HAVE ERRORS. FILL THE 2D ARRAY WITHOUT DUPLICATES VALUES. AND DISPLAY IF THE ARRAY IS MAGIC SQUARE OR NOT.
View Replies
View Related
Apr 11, 2014
Write a method compressDuplicates that accepts a stack of integers as a parameter and that replaces each sequence of duplicates with a pair of values: a count of the number of duplicates, followed by the actual duplicated number. For example, suppose a variable called s stores the following sequence of values:
bottom [2, 2, 2, 2, 2, -5, -5, 3, 3, 3, 3, 4, 4, 1, 0, 17, 17] top
If we make the call of compressDuplicates(s);, after the call s should store the following values:
bottom [5, 2, 2, -5, 4, 3, 2, 4, 1, 1, 1, 0, 2, 17] top
This new stack indicates that the original had 5 occurrences of 2 at the bottom of the stack followed by 2 occurrences of -5 followed by 4 occurrences of 3, and so on. This process works best when there are many duplicates in a row. For example, if the stack instead had stored:
bottom [10, 20, 10, 20, 20, 10] top
Then the resulting stack after the call ends up being longer than the original:
bottom [1, 10, 1, 20, 1, 10, 2, 20, 1, 10] top
If the stack is empty, your method should not change it. You may use one queue as auxiliary storage to solve this problem. You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like. You may not use recursion to solve this problem. For full credit your code must run in O(n) time where n is the number of elements of the original stack.
I wrote a code but still having a problem with it , am I allowed to use 3 while loops ?
public void compressDuplicates(Stack<Integer> s ){
Stack<Integer> backup= new Stack<Integer>();
int count = 1;
while(!s.isEmpty()){
int temp = s.pop();
[Code] .....
// example 1
Expected output : bottom [5, 2, 2, -5, 4, 3, 2, 4, 1, 1, 1, 0, 2, 17] top
My output: //bottom [17, 2, 2, 2, 2, 2, -5, -5, 3, 3, 3, 3, 4, 4, 1, 0, 17, 17] top
View Replies
View Related
Jan 23, 2013
It's supposed to count all of the duplicates in an array and print out how many occurrences of the value starting at whatever index, or if there are no duplicates state that. Basically:
No duplicates with value 1 beyond Index 0
There are 3 more occurrences of value 2 starting at index 1
There are 2 more occurrences of value 2 starting at index 2....
This is what I've got so far:
Java Code:
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 2, 4, 3, 0, 5, 3, 2};
for(int i = 0; i<arr.length; i++){
int count = 0;
for(int j = i+1; j<arr.length; j++){
if((arr[j] == arr[i]) && (i!=j)){
count++;
System.out.print("There are " + count + " more occurrences of ");
System.out.println(arr[i] + " starting at index " + i);
}
}
}
} mh_sh_highlight_all('java');
View Replies
View Related
Sep 18, 2014
I am stuck on this exercise and I don't know what exactly is wrong. I think it's something with the .remove and the for each loop, but I am not sure.
public class seven {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("aaa");
list.add("brr");
list.add("unni");
[Code] ....
This is what i get
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at seven.removeDuplicates(seven.java:24)
at seven.main(seven.java:18)
View Replies
View Related
Apr 16, 2014
This code is not best way to find the duplicate elements in a given array. Any alternative method for an optimized code.
Java Code:
import java.util.Arrays;
public class Find_Dupliicate_ArrayElement {
public static void main(String[] args) {
int[] Array1 = {1, 9,8,1,2,8,9,7,10, -1, 1, 2, 3, 10, 8, -1};
// Store the array length
int size = Array1.length;
//Sort the array
Arrays.sort(Array1);
[code]....
View Replies
View Related
Apr 20, 2014
I have a HashSet, which I created to prevent duplicates upon output, but of course it's printing duplicates(or else I wouldn't be posting this). The order of my output does not matter, nor the input. The data type is String in the format (x + "," + z), where x and z are integers, creating a collection of coordinate sets. So to prevent the output of duplicates, I'm trying to get rid of the duplicates before they are added to the collection.
I've tried doing a '.equals()' string comparison but what happens is, since my string is added via one variable, it compares itself to itself and if itself equals itself it won't be added to the collection. I really need to keep this as a comparison of a single variable, because creating a key for each value would be sooo ridiculous for this volume of inputs.
So, with that being said, I would like to add one copy of the string, discard the duplicates, and do this thousands of times..
View Replies
View Related
Nov 30, 2014
I'm trying to remove duplicates from table. I want to remove only those rows which are both id and data equal. My code failed at fourth line.
int i=jTable1.getSelectedRow();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
jXDatePicker1.setFormats(dateFormat);
String date = dateFormat.format(jXDatePicker1.getDate()).toString();
String c1=jTable1.getValueAt(i, 0).toString();
String c2=jTable1.getValueAt(i, 1).toString();
[Code] .....
View Replies
View Related
Apr 30, 2015
How to sort data from a .csv file. The file has a column that contains duplicate groups, and a column that has duplicate employee id's. I need to take the data and sort it into rows. The employee's id will be in the first column, then the groups the employees belong in will occupy the following columns. The groups and employees are dynamic.
groups| empId
-----------------
Group A| a1234 |
Group A| e3456 |
Group A| w3452 |
Group A| d3456 |
Group A| j7689 |
[Code] ....
I want to format the .csv as follows:
--------------------------
empId | group 1 | group 2 |
--------------------------
a1234 | group A | group B |
---------------------------
w3452 | group A | group B |
---------------------------
View Replies
View Related
May 6, 2014
I need to call the method to remove duplicates form my array, but it won't let me call the method, or I'm doing it incorrectly which is probably it.
import java.util.*;
public class C_6_15_EliminateDuplicates {
public static void main(String[] args) {
int[] numbers = new int[10];
Scanner input = new Scanner(System.in);
System.out.println("Enter " + numbers.length + " numbers: ");
for (int i = 0; i < numbers.length; i++)
[Code] ......
View Replies
View Related
Nov 5, 2014
Say You have the following values in a stack
indent
escape
indent
paragraph
backtick
My requirement is to put in all unique strings and remove duplicates from the stack.The comparison has to be done only in Stacks.Use of any other Arrays ,Hash tables to store values or comparing is Prohibited.what is the minimum number of stacks required.
View Replies
View Related
Jul 11, 2014
The keys in a HashMap and the values in a Set must all be unique, but this can be circumvented when using custom objects in a HashMap and Set, because the compiler has no way to determine if the objects are equal or not, as shown in the example below:
Java Code:
import java.util.LinkedHashMap;
import java.util.Map;
public class HashCodeEquals {
public void run(){
Person p1 = new Person(1, "John");
Person p2 = new Person(2, "Matt");
Person p3 = new Person(1, "John");
[code]....
Obviously the equals method is needed because that compares the two objects. But why is the hashCode method needed?
View Replies
View Related
Jan 2, 2015
I have following code to populate a dynamic menu from categories retrieved from database
<jsp:useBean id="category" class="category.CatBean" scope="application"></jsp:useBean>
<c:if test="${applicationScope.cartList == null}">
<c:set var="catList" value="${category.categoryList}" scope="application"/>
</c:if>
<div id="categoryBrowse"><label>Browse by Category</label></div>
<div id="cssmenu">
[Code] ....
It works fine. But every time I click on a link on the menu, it duplicates the whole menu.
View Replies
View Related
Feb 28, 2014
I'm trying to create my own arraylist using Collection. My program doesn't do anything. Obviously, I haven't a clue.
import java.util.Collection;
import java.util.Iterator;
public class MyArrayList<T> implements java.util.Collection<T> {
private int size = 4;
private T[] mArray;
public MyArrayList(String[] args) {
[Code] ....
View Replies
View Related
Nov 7, 2014
How can I write a method that takes a string with duplicates letters and returns the same string which does not contain duplicates. For example, if you pass it radar, it will return rad. Also i would like to know how can I Write a method that takes as parameters the secret word and the good guesses and returns a string that is the secretword but has dashes in the places where the player has not yet guessed that letter. For example, if the secret word is radar and the player has already guessed the good guesses letters r and d, the method will return r-d-r.
View Replies
View Related
Feb 16, 2015
I made this "lesson" to get the result of a pyramide which I succeded but not understand why im affraid..The thing is I dont understand exactly how to count it.. Isnt "j" incrementing every loop ?? In my head it should be just one star per line( the height is set to 3)how to count from the first loop to the last?I understand that println change the line though, but not why j would loop more than one time on each line..
for(int i = 1; i <= height; i++){
for(int j = 0; j < i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
result:
*
**
***
View Replies
View Related
Mar 6, 2014
How to do a count up timer in java? like "0:00:00" hh:mm:ss
View Replies
View Related
May 2, 2014
I have having some trouble on counting the primitive operations on the pseudocode given below:
Algorithm 4. MaximumArray(Arr)
Input: A 1-D numerical array Arr of size n
1) Let CurrentMax = a0
2) For i = 1 to n-1
3)If ai > CurrentMax Then CurrentMax = ai
4) End For
Output: CurrentMax, the largest value in Arr
As of now, I know that for Line 1 there are 2 operations (one set and one read). I don't know how to figure out the for loop and If statement (line 2 and line 3 too).
View Replies
View Related
Feb 13, 2015
I am currently trying to count and display all the vowels in a set of given strings and can't seem to figure out what to do. I was able to print the line with the most vowels, but i also need to display them. The code is listed below and the given output.
public class Strings
{
public static void main(String[] args) {
String sentence = "I am currently studing Computer Science."
+ "My name is whatever and I am orginaially from the state of Virginia."
+ "I recenetly separated from the Air Force where I served on the Presidental Honor Guard.";
[Code] ....
The output that i am getting is:
I am currently studying Computer Science .
My name is whatever and I am originally from the state of Virginia.
I recently separated from the Air Force where I served on the Presidential Honor Guard.
The line with most vowels is:
I recently separated from the Air Force where I served on the Presidential Honor Guard.
View Replies
View Related
Feb 28, 2014
(Count occurrence of numbers) Write a program that reads the integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0. Here is a sample run of the program:
Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time
Note that if a number occurs more than one time, the plural word "times" is used in the output.
View Replies
View Related
May 25, 2014
I am currently take a class, and trying to increment the count of employees I enter into an ArrayList by 1 for every entry. However, instead of incrementing by 1 each time, the current number of employeeIDs adds itself to itself, then adds 1, giving a non-sequential number (i.e. an employeeID of 75 will add itself to 75 + 1 to get a new employeeID of 151).
The following code is what I have done in terms of incrementing. (More coding has been done, but it is not used to increment, so I have omitted it.
public class Employee
{
// instance variables
private String firstName;
private String lastName;
private int employeeID;
static int newEmployeeID = 0;
[Code] ....
View Replies
View Related