Remove duplicates from integer array

前端 未结 23 2425
执念已碎
执念已碎 2020-12-01 18:52

I having a problem with coding this:

Write a static method named removeDuplicates that takes as input an array of integers and returns as a result a new

23条回答
  •  感情败类
    2020-12-01 19:15

    You can use HashSet that does not allow dulplicate elements

    public static void deleteDups(int a []) {
    
        HashSet numbers = new HashSet();
    
            for(int n : a)
            {
                numbers.add(n);
            }
    
            for(int k : numbers)
            {
                System.out.println(k);
            }
            System.out.println(numbers);
        }       
    
    public static void main(String[] args) {
        int a[]={2,3,3,4,4,5,6};
                RemoveDuplicate.deleteDups(a);
    
    }
    
    }
    o/p is 2
    3
    4
    5
    6
    

    [2, 3, 4, 5, 6]

提交回复
热议问题