File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes URGENT: Checking An Array For The Existance Of A Value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "URGENT: Checking An Array For The Existance Of A Value" Watch "URGENT: Checking An Array For The Existance Of A Value" New topic

URGENT: Checking An Array For The Existance Of A Value

Alan Jackson

Joined: Oct 26, 2004
Posts: 26

Im really pressed for time, and need to loop round an int array to see if any of the values appears more than once.

For example, I need to check that the number 5 is in element 0 only. Can anyone help?

Im appreciative of all assistance!
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Add the elements of this array into a Set (using wrapper classes). If the add() method returns true, the added element is not a duplicate, else, it is a duplicate.

Not sure whether this is the best way to do, but it works.

Quaerendo Invenietis
Sameera Abeysinghe
Ranch Hand

Joined: Nov 15, 2004
Posts: 104
Just type the below program and run it
class B{
int aa=5;
int [] a=new int[]{1,5,3,4,5,6,7,8,9,5};
int b;
void m(){
for(int x=0;x<a.length;x++){
System.out.println("The "+a[x]+" Element is "+a[x]);
System.out.println("Integer 5 "+b+" times");

class A{
public static void main(String [] a){
B b=new B();
Insert deferent values to the array
Assign the value you want to find to variable �aa�
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

not sure what your requirements are, but you could do it this way... if you're trying to find out if there are NO DUPLICATES in the array, you could do this

in other words, start at element 0. see if any element 1 - end match. if so, you can quit. if not...
start at element 1. see if it matches any element 2-end... etc.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Mani's solution is ok, but requires you to allocate an object for every element of the int array; Fred's is a bit brute-force as it has performance proportional to the square of the size of the array. A third possibility would be to sort the array (in time N ln N) and then scan through it looking for adjacent equal elements.

[Jess in Action][AskingGoodQuestions]
Anandh Ramesh
Ranch Hand

Joined: Dec 15, 2004
Posts: 61

i would recommend the sorting stuff for long arrays, but this seems to be a person with little knowledge on loops. further, i believe the simplest sorting (bubble sort) also has square of the loop size for working. so, that may take even longer. moreover, sorting with lesser complexity will require more coding, which i don't think we can expect from a guy new to java. i guess the brute force comparison will work best for our guy here. though elementary, it's effective.

All the Best,

cheers,<br />Anandh
I agree. Here's the link:
subject: URGENT: Checking An Array For The Existance Of A Value
It's not a secret anymore!