0

I am new to Java, With respect to this question remove same values from an array How can we achieve it using java. Any help would be greatly appreciated?

Community
  • 1
  • 1
jsduniya
  • 2,464
  • 7
  • 30
  • 45

3 Answers3

2

You could use a Set (e.g. HashSet), or a Map (e.g. HashMap). For each value in the array, just place it in the Set (or as a key in the Map).

You will then have a collection without duplicates (you can transform it back to an array if you need).

Alexey
  • 318
  • 1
  • 2
  • 10
0

Actually, you should use HashSet, it will remove duplicate values.

For sorting, you should use TreeSet.

leo.fcx
  • 6,137
  • 2
  • 21
  • 37
arti
  • 1
  • 3
0

You can achieve this simply by using the collections.

use set or HashMap and put the elements in the collections. when you retrieve you will get the unique elements .

If you are looking for pattern matching... better for the RegEx.

Shailej Shimpi
  • 145
  • 2
  • 3