String Comparisons / HashSet And Duplicates
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
ADVERTISEMENT
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
Apr 17, 2014
I want to generate a Hash key for a [ Nested Hierarchical nodes]. Any lightweight non-cryptographic hash map functions which generate a key, so that I can use this hashkey to comparison purposes.
NOTE: Security is not a concern for me, I just want to create a Unique ID.
View Replies
View Related
Sep 24, 2014
I am having a hard time getting some comparisons with Date to function correctly.I have a driver program and two classes, ZodiacDriver , ZodiacSign and ZodiacTable. In the driver program I build an arraylist of zodiacsigns in zodiactable. That all appears to be working I then pass a birthdate and the zodiac table to a method, determineSign. This is where I am having trouble. Right now Im just trying to match the incoming birthdate with a zodiac's date in the table. however no matter what date I enter, i get pisces, which happens to be at the bottom of the list. If I remove pisces from the list I get the current last zodiac. Here is my code:
ZodiacDriver
Java Code: package zodiac;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
[code]....
View Replies
View Related
Feb 5, 2015
I have three sorting algorithms in which I must count the number of swaps/copies and comparisons. I am meant to only count the swaps and comparisons that involve anything other than indexes as they are too fast to really matter (according to the professor). My counters are in the right position or not. I keep coming up with swaps/comparisons that don't necessarily match the formulas I'm finding for best/worst case. Makes me think that my counters are somehow out of place or that I don't have enough of them.
Insertion Sort:
public class InsertionSort {
public static void insertionSorter(int[] array) {
int firstValue; // first value in array
int scan; // scan array through the array
int moves = 0; // number of moves
[Code] ....
View Replies
View Related
Feb 24, 2014
My objective is to execute quick sort ( i was told to convert the pseudocode from the Cormen book) using arrays of increasing sizes and find the average number of comparisons for each of those sizes over 100 iterations. This is a school project and the numbers I am getting are far larger than those of my friends, so I am clearly doing something wrong. I believe it must be in the way that I am collecting and averaging my number of comparisons. I will first give the method in which most of that calculating is done, then I will include the whole program.
public static void tests(int arraySize) {
long numComparisons = 0;
long averageComparisons = 0;
long[] numComparisonsArray = new long[100];
for(int i = 0; i<100; i++) {
int[] array= genRandomArray(arraySize);
[code]....
I was simply not zeroing out one of my variables
View Replies
View Related
Oct 3, 2014
I have to count the comparisons in the quick sort. I have done it by using a global variable. But I am stuck in doing it recursively.
This is my code.
import static java.time.Clock.system;
import java.util.Arrays;
public class test {
/**
* The swap method swaps the contents of two elements in an int array.
*
* @param The array containing the two elements.
* @param a The subscript of the first element.
* @param b The subscript of the second element.
*
*/
[Code] .....
View Replies
View Related
Dec 10, 2014
A file has the data of the subjects and name of professors. The file contains line for each subject. The line starts with professor Name followed by the Subject name.
Harry Williams Advanced DBMS
James H Computer Networks
Sasha Ben Artificial Intelligence
Harry Williams Software Engineering
Palbo Kastro Formal Languages
Alex W Advanced SE
James H Operating System
Harry Williams Theoretical Foundation
Write a program with setter getter method to read the file, and then write a class to store several professors in a hashset (Single Key and Multiple Values).The program should be able to display all the professors in the set and allow the user to search for a professor.
Input:
Professor Name: James H.
Then the Output will be:
Subjects Taken by the professor: Computer Networks, Operating System.Display No Classes available if the professor name does not exists .
View Replies
View Related
Mar 23, 2015
I am trying to write a hashset to a text file. Normally, I have no issues with writing to txt as it is simple for me. However, in this case I am absolutely clueless.
A bit of background about the class: it is a hashset of ComputerScientist objects with 2 fields; name, and field of research (and yes, I know what I put to fill up the HashSet does not count, I was only trying to test to see if I could get this to work).
I know the basic setup to use filewriter to save strings to a txt, but I am getting confused with the HashSet element of it.
import java.util.HashSet;
import java.io.FileWriter;
import java.io.IOException;
/**
* Write a description of class ComputerScientistSet here.
*/
public class ComputerScientistSet {
private HashSet<ComputerScientist> computerScientistSet;
[Code] .....
View Replies
View Related
Nov 18, 2014
Having trouble adding Class (Dollar) objects to a HashSet (money), i have done this before with arraylists and i understand that HashSets are different in that they cannot contain duplicates. Currently when this code is compiled i am getting "null" printed when I run the "howFullDatWallet" method.
import java.util.*;
public class Wallet {
private HashSet<Dollar> money;
private int walletSize = 0;
private int walletFiller = 0;
/**
* Constructor for objects of class Pocket
*/
public Pocket(int walletCap)
[code]....
View Replies
View Related
Mar 12, 2014
What is the difference between HashSet, TreeSet and LinkedHashSet?.One difference between HashSet and TreeSet is that TreeSet is sorted, whereas HashSet is not sorted. I dont know the other differences
View Replies
View Related
Apr 3, 2015
I'm fairly new to java, and is just building small programs to get the feel for the language. How I can improve this code.
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
enum DateValidator {
DDMMYYYY(0, 2, 4),
MMDDYYYY(2, 0, 4),
YYYYMMDD(6, 4, 0);
[Code] ....
View Replies
View Related
Jan 12, 2015
Is there any way to find how many number of elements are landing in same bucket in HashSet. I know Reflection could be one way but i am not able to design a program for that.
View Replies
View Related
Sep 23, 2014
I'm not new to java but i'm not able to solve the following issue: I have a class
public class Localizzazioni implements java.io.Serializable {
private <complexType> id;
public getId().......
public setId().....
The complexType is a class defined in the code somewhere. Now I want to access it in another class I have
Set localizzazioni = new HashSet(0);
localizzazioni=opere.getOiLocalizzazioneOperas(); -- this object give an object of tyoe HashSet
for(Object object : localizzazioni) {
object.get......... // i cannot use any method defined in the class Localizzazioni
}
Why I cannot write inside the for object.getId() and using it?? In other word how i can access the element contained in the object?? the object is an iterator of type Localizzazioni . The class Localizzazioni has some method but i cannot use them? why ....
View Replies
View Related
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
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
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 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 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
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
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