Fill 2D Array With No Duplicates
Nov 2, 2014THE PROGRAM DOES NOT HAVE ERRORS. FILL THE 2D ARRAY WITHOUT DUPLICATES VALUES. AND DISPLAY IF THE ARRAY IS MAGIC SQUARE OR NOT.
View RepliesTHE PROGRAM DOES NOT HAVE ERRORS. FILL THE 2D ARRAY WITHOUT DUPLICATES VALUES. AND DISPLAY IF THE ARRAY IS MAGIC SQUARE OR NOT.
View RepliesIt'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');
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)
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
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]....
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] ......
I need to write a program but I am having hard time filling an array of triangles...we are suppose to have 3 different classes..1 class of points..1 class of triangles and 1 main method which apply all those..here what I have been come up so far for the point class which I believe is good..
public class Point {
private float x;
private float y;
//Making x equal to the x coordinate and y equal to the y coordinate like on a graph
public Point(float xCoordinate, float yCoordinate) {
x = xCoordinate;
y = yCoordinate;
[Code] ....
Now here is my question how I am suppose in the main method fill an array of triangles for 100 triangles or less?
my validAccounts array will not fill properly and has a null value in it.I added a print statement that prints the contents of the array and it is filling correctly but the very last value is null. I am supposed to be asking the user for a account number and password and it is not printing out correctly due to the fact that the array is not filled correctly.
import java.util.*;
import java.io.*;
public class ATM2 {
public static Scanner kbd;
public static final int MAXSIZE = 50002;
[code]....
I must fill a 2d array randomly and then apply methods to the array. However i keep getting an out of bounds exception no matter what dimensions i use. I have a test and a class program.
import java.util.*;
import java.lang.Math;
import java.util.Arrays;
import java.util.Random;
class SummerStats {
Random rand = new Random();
[Code] .....
My validAccounts array has a null value in it when i print it. How to do this.
I am NOT allowed to use bufferedReader or ArrayLists. This must be done with loops.
import java.util.*;
import java.io.*;
public class ATM2 {
public static Scanner kbd;
public static final int MAXSIZE = 50002;
public static void main(String[] args) {
kbd = new Scanner(System.in);
[Code] ....
I am attempting to make a 5x5 bingo board. My array generates 75 numbers(15 per bingo letter) and displays them in a random order. What I am having trouble figuring out is how to fill the 5x5 grid with 25 numbers from my array. And yes, my code is probably much longer and much more redundant than it needs to be. Also, I think I'm using cells and grids terribly wrong (possibly the entire java language) but I'm not sure.
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.event.*;
import java.util.ArrayList;
import java.awt.GridLayout;
import javax.swing.JButton;
public class bingoboard extends JFrame
[code]...
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?
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
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);
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..
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] .....
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 |
---------------------------
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.
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?
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.
We get a database and some DAO classes. We had to fill a ShowPanel with different shows being given in a theatre, and then when you choose from this you get several dates. Then when you click a date, the seatings appear in the SeatPanel. I got the first part with the shows and dates. But Im stuck at getting the seatings.We have a controller class which calls the shows+dates (this happens in one method). But i always get a nullpointerexception for the seatings. (this happens in a different method). I think i just don't know how to link the chosen show+date with the seatings.For the moment i have
public ArrayList<Seats> getSeating() throws SeatingException{
Show s = shows.getName();
ArrayList<Showdate> showdate = s.getDates();
ArrayList<Place> seating = new ArrayList<Place>();
try{
for (Showdate sd : showdate ){
[code]...
The given class PlaceDao has a static getInstance() method and an ArrayList<model.Place> getSeating(model.Showdate showdate) method.In the next stage i have to try and fill the panel with the seatings, but first i need to be able to call them.
I need to make a string filled with naughts and crosses like this one : "xxx ooo xox". There are 3 groups separated with a space. how to fill the string randomly ?
View Replies View RelatedI would like to make a small gift for my dad and i have a spreadsheet and i would like to make a small program that asks information and the puts it in the right column in the spreadsheet .For example i ask how many children do you have in your class and you answer 7 and that number goes into the spreadsheet.
View Replies View RelatedI have to write an example program to fill boxes most efficiently with bags. All bags are the same size. The boxes come in Large, Medium and Small. Large boxes hold 20 bags. Medium boxes hold 10 bags. Small boxes hold 5 bags. I am getting input from JOptionPane input dialog box and then parsing that input to an integer.
I have to fill the large and medium boxes completely. I am not sure how to do this without an if statement. I can use one, but we have not covered it in class, so I am skeptical about using one to solve the problem. If I just divide the input number with integer division, I'm not going to get the remainder, right? So I'm thinking that I can mod my input number by 20 (the number of bags the user inputs) and then mod that number by 10 and then mod that number by 5, would that work?
Why this code is not working/my JTable won't updated after i click the button
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
[code]....
So, let's say I have a pdf that looks like an exam or anything that has answer choices and an oval to fill in.
So, if I want to create a program that fills in some of those ovals, I would first need to find the coordinates? I can use something like gimp?
And once I have those coordinates, what do I do? Like do I extract the oval image and replace it with a filled in one? Is there a library that lets me do this - also lets me set the size to make sure the new filled in oval image is the same size?