how to remove duplicate elements from an array?????
eg...input..{1,2,3,4,5,1,2,3,4,6,7}
output......{1,2,3,4,5,6,7}
Printable View
how to remove duplicate elements from an array?????
eg...input..{1,2,3,4,5,1,2,3,4,6,7}
output......{1,2,3,4,5,6,7}
Create a Set of somekind (HashSet perhaps), add all the elements to the Set. "Set" ensures there is only 1 copy of each unique instance.
Uniqueness is determined via the "equals" method of the class in question. For Integer, "equals()" will return true for instances that have equal intValues().
Easiest method would be to create a new array and copy the contents of the old one into it 1 by 1. if an element is repeated, it is not added into the new array.
if you don't want to create a new array, then u use a for-loop to search through the entire array for repeated elements and replace them with zero. then print all non-zero elements