Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Finding Duplicate Elements in an Array

Java Example – Finding Duplicate Elements in an Array

Java Examples Java Examples

The following example demonstrates how to find duplicate elements in Java:

Main.java File

public

class

MainClass

{

public

static

void

main

(

String

[

]

args

)

{

int

[

]

my_array

=

{

1

,

2

,

5

,

5

,

6

,

6

,

7

,

2

,

9

,

2

}

;

findDupicateInArray

(

my_array

)

;

}

public

static

void

findDupicateInArray

(

int

[

]

a

)

{

int

count

=

0

;

for

(

int

j

=

0

;

j

<

a

.

length

;

j

++

)

{

for

(

int

k

=

j

1

;

k

<

a

.

length

;

k

++

)

{

if

(

a

[

j

]

==

a

[

k

]

)

{

count

++;

}

}

if

(

count

==

1

)

System

.

out

.

println

(

"

Duplicate elements:

"

a

[

j

]

)

;

count

=

0

;

}

}

}

The output of the above code is:



重复元素 : 5

重复元素 : 6

重复元素 : 2

Java Examples Java Examples