Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is there any simple way to remove duplicate elements in Java(will two loops work and how). Thank you:)

IN: int[] arr = {1,3,4,2,3,1,6,7,7};
Output i want is:
{1,3,4,2,6,7}

the only i know is we can traverse it through loop.
 eg.
  for(int i = 0;i < arr.length;i++){
   for(int j = 0;j<arr.length;j++){

        if(    ){
          //what logic i can apply here.      
          }
     }       
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 Answer

This should work..

final Integer[] noDuplicates =
    new LinkedHashSet<>(Arrays.asList(arr)).toArray(new Integer[0]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...