6 Replies - 1664 Views - Last Post: 01 December 2011 - 12:16 PM Rate Topic: -----

#1 HicheKh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

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

#2 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 HicheKh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Thank you for the reply.

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.
Was This Post Helpful? 0
  • +
  • -

#4 HicheKh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • 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

Was This Post Helpful? 1
  • +
  • -

#6 HicheKh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 HicheKh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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++)
			array[i] = file.readInt();
		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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1