Find duplicate elements in an array

public class array_findDupicateInArray {
    public static void main(String[] args) {
        int[] array={1,2,5,6,7,9,2};
        findDupicateInArray(array);
    }
    public static void findDupicateInArray(int[] array){
        for(int i=0;i<array.length;i++){
            for(int j=i+1;j<array.length;j++){
                if(array[j]==array[i]){
                    System.out.println("重复元素为:"+array[j]+",位置在:"+j);
                    break;
                }
            }
        }
    }
}

The output result is:

Repeat element: 2, position: 7, repeat element: 5, position: 3, repeat element: 6, position: 5, repeat element: 2, position: 9

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>