Printing distinct integers in an array

后端 未结 8 1970
遇见更好的自我
遇见更好的自我 2020-12-22 06:17

I\'m trying to write a small program that prints out distinct numbers in an array. For example if a user enters 1,1,3,5,7,4,3 the program will only print out 1,3,5,7,4.

相关标签:
8条回答
  • 2020-12-22 06:59

    Put them in a set ordered by insertion time, then convert back to an array if necessary.

    new LinkedHashSet<Integer>(array).toArray()
    
    0 讨论(0)
  • 2020-12-22 07:06

    Either use a Set as other people have suggested or use an List compatible class. With a list compatible class just use the Contains method to check if it already exists in the array.

    0 讨论(0)
提交回复
热议问题