# how to find whether certain elements in array are equal to one another

Page 1 of 1

## 6 Replies - 2805 Views - Last Post: 01 December 2011 - 12:16 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=257662&amp;s=c21b7f05f5fdf81ceed625562ed46022&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 HicheKh

Reputation: 0
• Posts: 16
• Joined: 20-November 11

# how to find whether certain elements in array are equal to one another

Posted 29 November 2011 - 10:44 AM

Basically, consider this array:

```int[] array = { 1, 0, 4, 1, 5, 3, 7, 8, 9, 1, 4 };
```

Say I want to find if two or more elements of the array are equal. I want to loop over the elements and check for duplicates.

```          for (int i = 0; i < array.length; i++)
{
for (int j = i + 1; j < array.length - i; j++)
{
if (array[i] == array[j])
// do something
}
}
```

Is this how we approach it or not? And

And another question: if we want to remove the duplicates, how exactly do we do that? A pseudo-code or something would be helpful.

Is This A Good Question/Topic? 0

## Replies To: how to find whether certain elements in array are equal to one another

### #2 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: how to find whether certain elements in array are equal to one another

Posted 29 November 2011 - 11:21 AM

Yes that is one way to approach it. There are other more advanced and tricky ways but, your way will do the trick.

For your second question is a little more difficult. Because you can't change the size of an array will probably need to recreate it.

Try this to get you goin:
• Determine what value has duplicates
• Count the duplicates
• Create a new array sizeOf original minus the count
• Loop through array adding all the values into the new ray
• Check for the number you are working with; after the first time, dont add it in

This post has been edited by SwiftStriker00: 29 November 2011 - 11:22 AM

### #3 HicheKh

Reputation: 0
• Posts: 16
• Joined: 20-November 11

## Re: how to find whether certain elements in array are equal to one another

Posted 29 November 2011 - 12:17 PM

Well, my method doesn't quite give me the results I want.

```public class Duplicate
{
public static void main(String[] args)
{
int[] x = { 1, 0, 4, 1, 5, 3, 7, 8, 9, 1, 4 };

for (int i = 0; i < x.length; i++)
{
for (int j = i + 1; j < x.length; j++)
{
if (x[i] == x[j])
System.out.println("Duplicate");
}
}
}
}
```

This prints Duplicate 4 times; however, I want it to print "Duplicate" only two times, since there are three 1s and two 4s. Also, I want it to print the element that has duplicates.

### #4 HicheKh

Reputation: 0
• Posts: 16
• Joined: 20-November 11

## Re: how to find whether certain elements in array are equal to one another

Posted 29 November 2011 - 12:27 PM

Oh, and how many times that element is repeated.

Thank.

### #5 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: how to find whether certain elements in array are equal to one another

Posted 29 November 2011 - 12:37 PM

ArrayList<int> duplicatedElements = new ArrayList<int>();
When you find the element, you want to duplicate, instead of printing out, you canduplicatedElements.add( x[i] );

ArrayLists dynamically build over as you add/remove from them. So at the end of the loop, you should have a list of two elements.(1,4)

This post has been edited by SwiftStriker00: 29 November 2011 - 12:37 PM

### #6 HicheKh

Reputation: 0
• Posts: 16
• Joined: 20-November 11

## Re: how to find whether certain elements in array are equal to one another

Posted 29 November 2011 - 03:05 PM

We just started learning about ArrayList; however, we cannot use it as of yet. We learned about the Arrays class. Is there a way to use that instead? Arrays are still a little vague to me.

Thank you for the reply, mate.

### #7 HicheKh

Reputation: 0
• Posts: 16
• Joined: 20-November 11

## Re: how to find whether certain elements in array are equal to one another

Posted 01 December 2011 - 12:16 PM

```public class RemoveDuplicates
{
public static void main(String[] args)
{
In file1 = new In(args[0]);
In file2 = new In(args[1]);
Out file3 = new Out(args[2]);

int[] array1 = toArray(file1);
int[] array2 = toArray(file2);

int[] array3 = merge(array1, array2);
for(int i = 0; i < array3.length; i++)
file3.print(array3[i] + " ");
}

public static int[] toArray(In file)
{
int [] array = new int[file.readInt()];
for(int i = 0; i < array.length; i++)
return array;
}

public static int array3Length(int[] array1, int[] array2)
{
int length = array1.length + array2.length;
for(int i = 0; i < array1.length; i++)
{
for(int j = 0; j < array2.length; j++)
{
if(i != j && array1[i] == array2[j])
length--;
}
}
return length;
}

public static int[] merge(int[] array1, int[] array2)
{
int x = 0;
boolean isDuplicate = true;
int[] array3 = new int[array3Length(array1, array2)];

for(int i = 0; i < array1.length; i++)
{
for(int k = 0; k < array2.length; k++)
{
if(array1[i] != array2[k])
isDuplicate = false;
else
{
isDuplicate = true;
break;
}
}
if(!isDuplicate)
array3[i - x] = array1[i];
else
x++;
}
concat(array3, array2);
return array3;
}

public static void concat(int[] array3, int[] array2)
{
for(int i = array2.length; i > 0; i--)
array3[array3.length - i] = array2[array2.length - i];
}
}
```

Okay, so after hours of coding, I came up with this. The In command takes in a file name (.txt) with the integers inside it and Out command prints the results in a new text file. However, this is not quite working.

The first file:

7 -> this is the number of integers
1 1 2 4 5 7 2

The second file:

8 -> this is the number of integers
4 5 4 6 7 8 7 9

Output:

1 1 4 5 4 6 7 8 7 9

It, somehow, worked with lesser integers.

The problem statement is:

Write a program RemoveDuplicates.java that takes three file names as command line arguments. The first two files contain a list of integers (the first line of each file contain the number of integers in that file). Your program should write to the third file the union of the numbers in the first two files (without any duplicates). Hint: Store the contents of the files in arrays first.

The In class: http://introcs.cs.pr...ib/In.java.html
The Out class: http://introcs.cs.pr...b/Out.java.html