Checking then removing duplicates in array

  • Thread starter Hiche
  • Start date
  • Tags
    Array
In summary, the conversation discusses different approaches to remove duplicates from an array, with the most common method being to move the remaining values down one position in the array. Another option is to create a new array and only copy the non-duplicates into it. The conversation also mentions the use of a binary search tree to remove duplicates.
  • #1
Hiche
84
0
Well, the title pretty much sums up my question.

Code:
         public static int[] removeDuplicates(int[] x)
	{
		for (int i = 0; i < x.length; i++)
		{
			for (int j = i + 1; j < x.length; i++)
			{
				if (x[i] == x[j])
					System.out.print("Found a duplicate!");
			}
		}
	}

I came up with this. This should check for duplicates but the result isn't quite what I want.

Consider this array:

Code:
int[] array = { 1, 2, 3, 4, 5, 1, 2, 6, 1 };

Well, this has three 1s and two 2s. I want it so it give me that we have three 1s and two 2s and removes them. How exactly do I do that?

Thanks.
 
Technology news on Phys.org
  • #2
What are you supposed to do when you find a duplicate? "Remove the duplicate" is on the vague side.
 
  • #3
Hiche said:
Well, this has three 1s and two 2s. I want it so it give me that we have three 1s and two 2s and removes them.
If the goal is to remove duplicates, than you'd only remove two 1s and one 2. Are you still stuck on how to "remove" a duplicate?
 
  • #4
Mark44 said:
What are you supposed to do when you find a duplicate? "Remove the duplicate" is on the vague side.

When a duplicate of an integer in the array is found, it should be removed from the array. Every duplicate of a number will be removed and a new array will be created without any duplicates.

rcgldr said:
If the goal is to remove duplicates, than you'd only remove two 1s and one 2. Are you still stuck on how to "remove" a duplicate?

Yes, I'm sorry I wasn't clear on that. Any repeating integer will be removed (not the integer itself). Yes, I'm still stuck on how to remove the duplicate, checking for duplicates is somehow working.
 
  • #5
In think in java, as far as I know, adding elements(objects) at the end of an array is easy. But inserting and deleting the elements in the middle of the array is difficult. In this case, we have to re-arrange all the elements of the array.(By using some sort of sorting techniques.) Then possibly you can process the elements in your case.
Remember that array holds elements of only one type of datatype. If you want different objects in a single array, you have to use Collections concept. You can use Collection concept even on arrays which have elements of single data type.
Also I think, retrieving the elements from an array is easy but after retrieving the elements, if we want to process them, then there are no methods in java to carry this out.
OR
The alternative or the only way could be to use wrapper classes to convert each element in an array to an object and use a 'different object' to store all of these objects. This 'different object' is called Collection object.The Collection object actually stores the references of other objects, not the physical copies of other objects. Collection object never stores primitive data type, stores only objects.That is why there is a concept called Wrapper classes introduced. Collection object belongs to Collection class. Collection class belongs to java utility package.
There are several Collection classes in java utility package to process these elements(objects). We are advised to use these classes at our discretion. We have to decide which Collection class(generic class) is best suited to process our requirement/problem.
I think you should go through the concepts of Collections thoroughly.
You should be familiar with Type casting, Wrapper classes and Collections.
 
Last edited:
  • #6
There are (at least) two different ways to get rid of duplicates.

A. Delete them from the existing array somehow.
B. Create a new array, and only copy the non-duplicates into it.

If you don't like "plan A", try working on "plan B" instead.
 
  • #7
Hiche said:
Yes, I'm sorry I wasn't clear on that. Any repeating integer will be removed (not the integer itself). Yes, I'm still stuck on how to remove the duplicate, checking for duplicates is somehow working.
Since you're stuck here, the most common method to remove a duplicate is to move the remaining values in the array down one position, so that the value to be removed is overwritten by the first of the remaining values. After doing this you need to consider the size of the array as reduced by 1. So now the question is how would you move the remaining values down one position within the array?
 
  • #8
You have already figured out how to, for each item in the array, find all duplicates of that item further in the array.
But instead you need to figure out how to determine whether or not an item should be added to the new array. When should an item not be added to the duplicate free array, if it already has some items in it?


"Plan B" probably better for you. It should be simpler than "Plan A" to implement. Also, typically many less operations will be required, so we'd expect "Plan B" to execute faster. The memory savings from using "Plan A," (an "in-place" algorithm) is not significant in this case.
 
  • #9
Suppose I want to find the duplicates in an array but I don't know how many elements I am going to give to the array. And while I enter the values it should display that I have entered duplicate elements. Here, I want to know how do I accept array elements dynamically. If it's not possible in array. Then is there any way in java that I can accept elements dynamically from the keyboard during run-time and so that I can prompt the user to enter a different value if the user entered a duplicate value?
Main question is: I want to enter elements during run-time in java and I don't know how many elements I am going to enter. How do I do that?
 
  • #10
Hiche, try moving all the duplicates to the end of the array.

If you don't need to resize the array, I would recommend you do the above approach.
 
  • #11
The problem could be solved with a Binary search tree.
A standard textbook implementation drops duplicate values on insertion.
 

1. What is the purpose of checking and removing duplicates in an array?

The purpose of checking and removing duplicates in an array is to ensure that the data within the array is unique and to avoid any unnecessary repetition. This can also improve the efficiency of the program by reducing the amount of data that needs to be processed.

2. How can duplicates be checked in an array?

Duplicates can be checked in an array by using a loop to compare each element in the array with the rest of the elements. If a duplicate is found, it can be removed or marked for removal.

3. What is the best approach for removing duplicates in an array?

The best approach for removing duplicates in an array depends on the specific programming language and the size of the array. In general, using a hash table or a set data structure can be an efficient way to remove duplicates as they do not allow for duplicate values.

4. Can duplicates be removed without altering the original order of the array?

Yes, duplicates can be removed without altering the original order of the array. This can be done by using an additional data structure, such as a temporary array, to store the unique elements in the original order.

5. Are there any downsides to removing duplicates in an array?

One potential downside of removing duplicates in an array is that it can increase the time and space complexity of the program. Additionally, if the original order of the array is important, removing duplicates may alter the expected output. It is important to consider these factors when deciding whether or not to remove duplicates in an array.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
13
Views
4K
Back
Top