Anyway, I have this extremely simple java program to find out if 2 objects in an array are the same. How ever when I run it when a set of unique objects it always returns 1 error, If I add more objects that are the same it counts as normal.
This is the code;
int[] array= new int[] {1,245,324,523};
int size = array.length;
int error=0;
System.out.println(error);
int i = 0;
for(i=0; i<size; i++){
if(array[0] == array[i]){
error= error +1;
}
System.out.println(error);
}
8 Answers 8
The 1 error is because you're comparing array[0]
with array[0]
, which is of course equal to itself.
If you want to find all pairwise duplicates, you will need to do a double loop:
for(int i=0;i<size;i++){
for(int j=i+1;j<size;j++){
if(array[i] == array[j]){
if(i!=j){
error = error + 1;
}
}
}
}
You'll notice a few things from this code:
- j starts at i+1, not at 0.
error
is only incremented when i!=j
The first is because you're taking turns with each element in your array to compare with every other element. By the time its turn comes around (in the outer loop), it's already been compared to the elements before it, and should not be compared with them again.
The second is because you'll end up comparing an element to itself for each outer loop. You don't want to count it as an error
.
-
1I think in your double loop j should always start at i+1.Hot Licks– Hot Licks2011年09月30日 00:07:42 +00:00Commented Sep 30, 2011 at 0:07
-
i<size-1 in the first for condition.Cory Kendall– Cory Kendall2011年09月30日 00:12:55 +00:00Commented Sep 30, 2011 at 0:12
-
that second if i!=j is unnecessarylukastymo– lukastymo2011年09月30日 00:15:11 +00:00Commented Sep 30, 2011 at 0:15
-
yeah, er. there's a slight problem, because the index would start at
size
for the last outer iteration. Surely our user can figure out what to do about it.user684934– user6849342011年09月30日 00:26:38 +00:00Commented Sep 30, 2011 at 0:26 -
All i'm aiming to do is find out if all the elements are distint or not, I think it would still work with out the outside loop how ever the counter would have the wrong number of errors. But if theyre are no errors it is impossible for errors to be higher than one?user972183– user9721832011年09月30日 00:58:17 +00:00Commented Sep 30, 2011 at 0:58
You're starting i at 0. Therefore the first test is if(array[0] == array[0])
;)
You're always going to get at least one error because array[0] == array[i]
will be true on the first iteration, when i = 0.
In your code at
int i=0;
for(i=0;i<size;i++){
if(array[0]==array[i]){ //this condition runs true only for the first time. as i=0 here
error=error+1;
}
System.out.println(error); //now here you had put the println(error) outside the if()-condition therefore it will be printed repeatedly value of error which is 1
}
Try a doubly nested for loop. Something like this
for (int i=0;i<size-1;i++){
for (int j=i+1; j<size; j++) {
error += (array[i] == array[j]) ? 1 : 0;
}
}
If you are going to make use of any collection, you can easiy findout the duplicates in the array
Example:
public static void main(String[] args) {
boolean containsDuplicate =false;
int[] intArray = new int[] {1,245,324,1,523};
List<Integer> myObj = new ArrayList<Integer>();
for(int id : intArray){
if(myObj.contains(id)){
containsDuplicate = true;
System.out.println("Duplicate");
}else{
myObj.add(id);
}
}
}
Following code is fine but it returns 11. Not sure if it is what was expected.
public static void main(String[] args){
int[] array = {23, 23, 0, 43, 545, 12, -55, 43, 12, 12, -999, -87, 12, 0, 0};
int error = 0;
for(int i=0;i<array.length;i++){
for(int j=i+1;j<array.length;j++){
if(array[i] == array[j]){
if(i!=j){
error = error + 1;
}
}
}
}
System.out.println(error);
}
I propose little bit complicated but hopefully more advanced solution.
package myjavaprogram;
import java.util.Arrays;
public class TestClass1 {
public static void main(String[] args){
int[] array = {23, 23, 0, 43, 545, 12, -55, 43, 12, 12, -999, -87, 12, 0, 0};
//int[] array = {23, -22, 0, 43, 545, 12, -55, 43, 12, 0, -999, -87, 12};
//int[] array = {23, -22, 0, 23};
//int[] array = {23, -22, 23};
calculate_duplicates(array);
}
private static void calculate_duplicates(int[] array) {
calculateUniqueNumbers(array);
}
private static void calculateUniqueNumbers(int[] array) {
Pair[] pairs = new Pair[array.length];
initializePairsAtrray(pairs, array);
printPairsAtrray(pairs);
System.out.println("array.length="+array.length);
System.out.println("--------------------");
// update pairs array taking in account duplicates duplicates
for(int i = 0; i < array.length; i++) {
System.out.println("array[i]="+array[i] + " i="+i);
for(int j = i+1; j < array.length; j++) {
System.out.println("array[j]="+array[j]+" j="+j);
if(array[i] == array[j] && pairs[j].useDuringCount == true) {
pairs[i].occurance_num++;
pairs[j].occurance_num = 0;
pairs[j].useDuringCount = false;
}
if(array[i] == 0) {
pairs[i].occurance_num = 0;
}
if(array[j] == 0) {
pairs[j].occurance_num = 0;
pairs[j].useDuringCount = false;
}
}
pairs[i].useDuringCount = false;
System.out.println("--------------------");
}
printPairsAtrray(pairs);
// calculate general number of duplicates (numbers whick are repeated
// in initial array)
System.out.println("Duplicates in array:"+
calculateDuplicatesNumber(pairs));
}
private static void initializePairsAtrray(Pair[] pairs, int[] array) {
for(int i=0;i<pairs.length;i++) {
Pair p = new Pair();
p.occurance_num = 1;
p.value = array[i];
p.useDuringCount = true;
pairs[i] = p;
}
}
private static void printPairsAtrray(Pair[] pairs) {
System.out.println("--------------------");
for(int i=0;i<pairs.length;i++) {
System.out.println("pairs["+i+"].occurance_num="+pairs[i].occurance_num);
System.out.println("pairs["+i+"].value="+pairs[i].value);
System.out.println("pairs["+i+"].useDuringCount="+pairs[i].useDuringCount);
System.out.println("--------------------");
}
}
private static int calculateDuplicatesNumber(Pair[] pairs) {
System.out.println("-------------------- Duplicates:");
int duplicates_num = 0;
for(int i=0;i<pairs.length;i++) {
if(pairs[i].occurance_num > 1) {
duplicates_num++;
System.out.println("number: "+pairs[i].value+" occurance_num " + pairs[i].occurance_num);
}
}
return duplicates_num;
}
}
class Pair {
int value;
int occurance_num;
boolean useDuringCount;
}
Mykola
-
The accepted answer, IMO, appears to do this simple task a lot more efficiently. No reason to add complexity to something solved with a much simpler and viable solutionChris– Chris2014年12月31日 17:20:34 +00:00Commented Dec 31, 2014 at 17:20
-
Add additional array. Next cycle through each elements of the original array. For current element, iterate over all elements of the array to the right of the current. If found equal, this element put in an additional array. You can make a 2nd array for additional calculations, or just use two-dimensional array.Mykola Bova– Mykola Bova2015年01月06日 11:10:59 +00:00Commented Jan 6, 2015 at 11:10
if(array[0]==array[i])
?