-1

I accept a mobile numbers array, and I need to remove repeated numbers in the array. The way I choose is converting the array to a hashset, then convert it back to an array. Is there any better way?

My code is below:

public static void main (String[] args) {

    String[] mobiles = new String[]{"1", "2", "3", "1", "1"};

    Set<String> data = new HashSet<>(Arrays.asList(mobiles));
    String[] result = data.toArray(new String[0]);

    for (String s : result) {
        System.out.println(s);
    }
}
niqueco
  • 2,261
  • 19
  • 39
wanglong
  • 89
  • 5
  • 7
    `Arrays.stream(mobiles).distinct().toArray(String[]::new)` – shmosel Apr 11 '19 at 05:20
  • @shmosel niu bi – wanglong Apr 11 '19 at 05:25
  • 1
    Welcome to Stack Overflow. Did you notice that you are supposed to search and research before posting a question? This topic has been covered many times already. [I downvoted because research must be done to ask a good question](http://idownvotedbecau.se/noresearch/). See [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) too. – Ole V.V. Apr 11 '19 at 06:31

4 Answers4

3

you can use Java8 Streams as well.

public static void main(String []args){
        String[] mobiles = new String[]{"1", "2", "3", "1", "1"};
        String[] result= Arrays.stream(mobiles).distinct().toArray(String[]::new);
        for (String s : result) {
               System.out.println(s);
        }                             
}
Mustahsan
  • 3,852
  • 1
  • 18
  • 34
2

Simplest way is to use a LinkedHashSet. This way, the order is kept:

String[] nonUnique = new String[] { "1", "2", "5", "2", "3", "1" };
List<String> unique = new ArrayList<>(new LinkedHashSet<>(Arrays.asList(nonUnique)));

Generally, I'd suggest using collections instead of arrays.

jokster
  • 577
  • 5
  • 14
0

I rather like your current approach. Off the top of my head, you could try to remove duplicates by first sorting the array, and then walking down the array:

int[] mobiles = new int[] {1, 2, 3, 1, 1};
Arrays.sort(mobiles);
List<Integer> result = new ArrayList<>();

int last = -1;
for (int val : mobiles) {
    if (val != last || result.size() == 0) {
        result.add(val);
        last = val;
    }
}

mobiles = new int[result.size()];
for (int i=0; i < result.size(); ++i) {
    mobiles[i] = result.get(i);
}
System.out.println(Arrays.toString(mobiles));

[1, 2, 3]

This answer assumes that you don't want to retain the original order in the array. Of course, "original" order only makes sense once you define which of the duplicates you want to retain.

The performance bottleneck on the above approach depends on how costly it is to sort the array. Assuming this can be done in O(n*lgn), then this would be the overall performance.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

You can use hashmap also for solving this one. Store all the different elements.

Hashmap<Integer,Integer> hm = new Hashmap<>();

    for(int i=0;i<arr.length;i++)
    {
           hm.add(arr[i],1);

    }
Now print the hashmap.
Rohit
  • 1