aspose file tools*
The moose likes Beginning Java and the fly likes help me with array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help me with array" Watch "help me with array" New topic
Author

help me with array

Manisekar Chinnasami
Greenhorn

Joined: Apr 07, 2007
Posts: 24
itz regarding the array ...
consider an array,

a[] = { 1,2,4,6,7,2,6};

i need to delete all the repeated values and the output such as ,

a[] = {1,4,7} ...

the output should b stored in the same array ....

help me to slove this one ...


Manisekar
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

We're here to help, but we're not going to do it for you. Do you have any ideas about how to accomplish this?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Originally posted by Manisekar Chinnasami:
the output should b stored in the same array ....

Then what are you going to store in the extra spaces? In your example, the array originally holds 7 elements. Afterwards it should hold 3 elements. But an array cannot be resized; it would contain these 3 elements and 4 other elements - what should these be?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Manisekar Chinnasami
Greenhorn

Joined: Apr 07, 2007
Posts: 24
i ve tried them in many ways ... but i cudnt sort out ...

here is one algorithm ...

for i = 1 to lenthofarray
for j = 1 to lenthofarray
if(i != j)
if(a[i]==a[j]
{store in b array
break;}

but the value is not stored in b array ... i ve tried in this way ... i need the not equal values to b stored in a array ...

(i m trying this for Reed-Muller's logic)
now guide me ...
Manisekar Chinnasami
Greenhorn

Joined: Apr 07, 2007
Posts: 24
yup... tat is also a problem ... i even need them to resize ...

(actually, i m storing the binary values ... so zero is also an value)
Ajith George
Ranch Hand

Joined: Dec 22, 2005
Posts: 109
Better store the result in a new array, which will make your life more easier.


SCJP 1.4, Brainbench
LinkedIn - Blog
Manisekar Chinnasami
Greenhorn

Joined: Apr 07, 2007
Posts: 24
this is not the whole program ... itz just a part ...

the a[] array is processed in a loop with the resulted array values ... if i use a large loop of 15 or 20, then do i ve to create 15 to 20 arrays ???

so storing in a different array is not possible ...

i need them to be stored in the same array ...
Ajith George
Ranch Hand

Joined: Dec 22, 2005
Posts: 109
Then replace one of the repeated entries with a non occuring number , like 999 or 0. Then while retrieving it handle this case also.
Jitendra Takalkar
Greenhorn

Joined: Jan 12, 2007
Posts: 9
Hi,

Please find the TestAray class which elemets if any duplicate value ..


This can be optimized ....

public class TestArray {

public static void main(String[] args) {
int[] myarray= {1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8};

int duplicateCount=0;
for(int i=0; i<myarray.length; i++) {
for(int j=i; j<myarray.length; j++) {
if(i!=j) {
if(myarray[i]==myarray[j]) {
duplicateCount++;
}
}
}
}

if(myarray.length!=duplicateCount) {
int[] newarray=new int[myarray.length-duplicateCount];
int k=0;
for(int i=0; i<myarray.length; i++) {
boolean isFound=false;
for(int j=0; j<newarray.length && !isFound; j++) {
if(myarray[i]==newarray[j]) {
isFound=true;
}
}

if(!isFound) {
newarray[k++]=myarray[i];
}
}
/*
* Output elements of new array.
*/
for(int i=0;i<newarray.length; i++) {
System.out.println(newarray[i]);
}

myarray=newarray;
}

/*
* Output of old array
*/
for(int i=0; i<myarray.length; i++) {
System.out.println(myarray[i]);
}
}

}
vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
Posts: 169
Maybe if you are allowed you can use Sets ...
This will not allow duplicate elements

1.Take each element from the Array
2.Add to a set.
3.Convert it back to Array
Manisekar Chinnasami
Greenhorn

Joined: Apr 07, 2007
Posts: 24
not only the duplicate values should be deleted ... if a value is repeated , then both of them should be removed from the array ...

a[] = { 1,2,3,5,1,3,2}
then, the answer should be a[] = {5}

only 5 should be printed ...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Manisekar Chinnasami
Greenhorn

Joined: Apr 07, 2007
Posts: 24
HELLO FRIENDS,

FINALLY I GOT THE SOLUTION FOR THIS QUERY ...

THANKS FOR YOUR HELP ...

HERE IS THE PROGRAM ...

class Arr
{
public static void main(String[] args)
{
Arr ao = new Arr();
int a[] = {1,2,3,2,1};
int k=0;


for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
if(i!=j)
if(a[i] == a[j])
{
k++;
break;
}
}

int b[] = new int[k];
k=0;

for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
if(i!=j)
if(a[i] == a[j])
{
b[k] = a[i];
k++;
break;
}
}

int g,m=0;
for(int l=0;l<a.length;l++)
{
g = 0;
for(int j=0;j<b.length;j++)
{

if(a[l] == b[j])
{
break;

} g++;
if(g == b.length)
{ a[m] = a[l];
m++;
}

}
}

for(int i=0;i<m;i++)
System.out.println(a[i]);
}
}
 
 
subject: help me with array
 
Similar Threads
updating array elements
Vector of string arrays contains same values
how to remove the [[ from arraylist
Image IO problem
sending array of bytes through webservice