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

Object[] _newArray = new Object[currentIndex];

System.arraycopy(newArray, 0, _newArray, 0, currentIndex);

}

User Avatar

Wiki User

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

Wiki User

11y ago

printf( "Enter 20 integers between 10 and 100:\n");

15

16 /* get 20 integers from user */

17 for( i = 0; i <= MAX- 1; i++ ) {

18 duplicate = 0;

19 scanf( "%d", &value );

20

21 /* test if integer is a duplicate */

22 for( j = 0; j < k; j++ ) {

23

24 /* if duplicate, raise flag and break loop */

25 if( value == a[ j ] ) {

26 duplicate = 1;

27 break;

28 } /* end if */

29

30 } /* end for */

31

32 /* if number is not a duplicate enter it in array */

33 if( !duplicate ) {

34 a[ k++ ] = value;

35 } /* end if */

36

37 } /* end for */

38

39 printf( "\nThe nonduplicate values are:\n");

40

41 /* display array of nonduplicates */

42 for( i = 0; a[ i ] != 0; i++ ) {

43 printf( "%d ", a[ i ] );

44 } /* end for */

45

46 printf( "\n");

47

48 return 0; /* indicate successful termination */

49

50 }

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Basically you have to compare each array element to every other array element. This can be done with a nested loop. For example, in Java: for (int i = 0; i

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program to detect duplicate elements in array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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 write c program to perform sum of elements of matrix using pointers with functions?

i cant write


How do you write a java program to find the transpose of the matrix for the given elements?

You basically write a nested for loop (one for within another one), to copy the elements of the matrix to a new matrix.


Write a C program using dynamic memory allocation to find the sum of elements of a matrix?

Did you know that memory allocation is not needed to display the matrix? However, the C program is to find the sum of all the elements.


How do you write a program that calculates the sum of the matrix elements given numbers?

ring me and ill explain - 086 22222222222222227 ring me


HOW TO SOLVE 'C' problem?

1. write a 'c' program to read 4(four)numbers from a file 'BANK' and calculate the average of the numbers.Now print the calculated average in another output file 'AVERAGE' 2. write a 'c' program that finds the sum and average of inputted five integer numbers of the array using dynamic memory allocation function malloc(). 3. write a 'c' program to create simple elements 1,2,3,4 in the link list of 4(four)nodes and display the list's elements. 4. write a 'c' program to convert the expression (A+B)/(C+D) into postfix expression into stack.and then evaluate it for A=10,B=20,C=15,D=5 and display the stack status after each operation. 5. write a 'c' programto create a linked list implemented on an array containing the following numbers:1,2,3,3,3,4,4,9 and pack it to remove the duplicate numbers.so that only the following data are contained by the nodes:1,2,3,4,9


What tool is used to duplicate blocks on TheVoxelBox in Minecraft?

write&quot;/more&quot; in comands


How do you write 'fear' in a sentence?

I fear your teacher will detect that you did not write this sentence yourself.


How to write program for secant method in mathematica?

How to write a program for secant method by mathematica


How do you write Square program using vb?

write a vb program to find the magic square


Write a program to multiply 33 matrix.?

write a program to multily 3*3 matrix.


Write a program in Lex to eliminate white space and collect numbers as a token?

write a lex program to delete space from the program