answersLogoWhite

0


Best Answer

The simplest way of doing this is not very efficient, but provides a quick development time. You want to take all of the elements of the array, add them to a Set, then retrieve them as an array again. Note that this will probably not preserve the order of elements in the array.

{

Object[] originalArray; // let's pretend this contains the data that

// you want to delete duplicates from

Set newSet = new HashSet();

for (Object o : originalArray) {

newSet.add(o);

}

// originalArray is now equal to the array without duplicates

originalArray = newSet.toArray();

}

Now the efficient, and more correct, solution.

This one will create a new array of the correct size without duplicates.

{

Object[] originalArray; // again, pretend this contains our original data

// new temporary array to hold non-duplicate data

Object[] newArray = new Object[originalArray.length];

// current index in the new array (also the number of non-dup elements)

int currentIndex = 0;

// loop through the original array...

for (int i = 0; i < originalArray.length; ++i) {

// contains => true iff newArray contains originalArray[i]

boolean contains = false;

// search through newArray to see if it contains an element equal

// to the element in originalArray[i]

for(int j = 0; j <= currentIndex; ++j) {

// if the same element is found, don't add it to the new array

if(originalArray[i].equals(newArray[j])) {

contains = true;

break;

}

}

// if we didn't find a duplicate, add the new element to the new array

if(!contains) {

// note: you may want to use a copy constructor, or a .clone()

// here if the situation warrants more than a shallow copy

newArray[currentIndex] = originalArray[i];

++currentIndex;

}

}

// OPTIONAL

// resize newArray (using _newArray) so that we don't have any null references

String[] _newArray = new String[currentIndex];

for(int i = 0; i < currentIndex; ++i) {

_newArray[i] = newArray[i];

}

}

---------

The second version is correct in theory. However, if you deal with large two- or more- dimensional arrays, you are in trouble, as with each new element in the destination array you will have to search through a greater number of elements.

This is especially true if you look for duplicates in more than one element of the array, for example looking in columns 'a' and 'b' of array

a1 b1 c1 d1

a2 b2 c2 d2

a3 b3 c3 d3

Drop in performance is unbelievable if you go over approx 1,000 records with majority or records being unique.

I am trying to test a couple of different approaches for large arrays. If anyone is interested, let me know, and I will keep you posted.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

Sort the array then test adjacent members for equality.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to remove duplicate values in an array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What happens if there are duplicate values in the array during a linear search?

Duplicate values have no effect on linear search other than to increase search times. When searching for a value that is duplicated, the index of the first element that matches the given value is returned. If you wish to return all indices that match the given value, you must either search the entire array to build a new array of indices, or sort the array so that all duplicates are allocated contiguously and return the range of indices.


What is the algorithm using c to delete duplicate elements from an array?

To detect the duplicate, you will have to write a nested loop that compares each element with all the previous elements.To actually delete the duplicate, once you find it, you have to move over all the elements after the duplicate. If the order of the elements doesn't matter, it is faster to just move the LAST array element, overwriting the duplicate element. Use a variable to keep track how many elements of the array are "usable". For example, if your array had 10 elements, and you delete 1, the array size will still be 10... but (after moving the elements over) only 9 of those elements have useful information.


Can an array be a single column of values?

Yes.


How do you display 15 numbers in an ascending order using an array?

Sort the array then traverse the array, printing the element values as you go.


How do you find the quartile in an even array of values?

Divide the array in half and get the median of each half

Related questions

Does foreign key alllows null values and duplicate values?

A foreign can have null values and it can have duplicate values.A foreign can have null values and it can have duplicate values.A foreign can have null values and it can have duplicate values.A foreign can have null values and it can have duplicate values.A foreign can have null values and it can have duplicate values.A foreign can have null values and it can have duplicate values.A foreign can have null values and it can have duplicate values.A foreign can have null values and it can have duplicate values.A foreign can have null values and it can have duplicate values.A foreign can have null values and it can have duplicate values.A foreign can have null values and it can have duplicate values.


What is the condition of second largest number?

There is no such condition. The algorithm to locate the second largest number in an array of numbers is: Sort the array in descending order. Remove the duplicate values from the array. If there are two or more elements remaining, return the second number. If there is less than two elements remaining, return the NaN value (not a number).


What happens if there are duplicate values in the array during a linear search?

Duplicate values have no effect on linear search other than to increase search times. When searching for a value that is duplicated, the index of the first element that matches the given value is returned. If you wish to return all indices that match the given value, you must either search the entire array to build a new array of indices, or sort the array so that all duplicates are allocated contiguously and return the range of indices.


Can a foreign-key column can contain duplicate values?

yep foreign key can have duplicate values


What is the difference between numeric array and associative array?

Numeric array has numbers(+integers) that represent the values Associative array has strings that represent the values


What is the algorithm using c to delete duplicate elements from an array?

To detect the duplicate, you will have to write a nested loop that compares each element with all the previous elements.To actually delete the duplicate, once you find it, you have to move over all the elements after the duplicate. If the order of the elements doesn't matter, it is faster to just move the LAST array element, overwriting the duplicate element. Use a variable to keep track how many elements of the array are "usable". For example, if your array had 10 elements, and you delete 1, the array size will still be 10... but (after moving the elements over) only 9 of those elements have useful information.


How do you merge arrays in PHP?

To merge arrays in PHP all you have to do is use the array_merge() function like shown below: &lt;?php $array1 = array("Matt", "Michael", "Justin"); $array2 = array("Janice", "Janet", "Kylie"); $array3 = array_merge($array1, $array2); ?&gt; One thing to remember when merging arrays is you might be creating duplicate results, if you don't want 2 or more of the same result in the array remember to use the function array_unique() to remove the duplicate results from it!


What type of field cannot hold duplicate value in ms access?

A primary key field is always unique. Any other field that you have can be set to not allow duplicate values if necessary too.A primary key field is always unique. Any other field that you have can be set to not allow duplicate values if necessary too.A primary key field is always unique. Any other field that you have can be set to not allow duplicate values if necessary too.A primary key field is always unique. Any other field that you have can be set to not allow duplicate values if necessary too.A primary key field is always unique. Any other field that you have can be set to not allow duplicate values if necessary too.A primary key field is always unique. Any other field that you have can be set to not allow duplicate values if necessary too.A primary key field is always unique. Any other field that you have can be set to not allow duplicate values if necessary too.A primary key field is always unique. Any other field that you have can be set to not allow duplicate values if necessary too.A primary key field is always unique. Any other field that you have can be set to not allow duplicate values if necessary too.A primary key field is always unique. Any other field that you have can be set to not allow duplicate values if necessary too.A primary key field is always unique. Any other field that you have can be set to not allow duplicate values if necessary too.


Can an array be a single column of values?

Yes.


How do you display 15 numbers in an ascending order using an array?

Sort the array then traverse the array, printing the element values as you go.


How do you find the quartile in an even array of values?

Divide the array in half and get the median of each half


What is the main reason to use inheritance?

to avoid the duplicate values