Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

delete replicating values from an array[]....

Question posted by: aswath (Newbie) on March 26th, 2008 01:40 PM
hi dere,
i have a question.
i have an array with replicating values.... al i want to do is
delete all replicating values and store the result in another array.
eg.,
String[] a={1,2,3,3};
String []b;
b should be 1.2.3only........not 1,2,3,3
thanks in advance,
regards,
aswath.n.s
(knowledge is power)
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
sukatoa's Avatar
sukatoa
Needs Regular Fix
459 Posts
March 26th, 2008
01:43 PM
#2

Re: delete replicating values from an array[]....
Quote:
Originally Posted by aswath
hi dere,
i have a question.
i have an array with replicating values.... al i want to do is
delete all replicating values and store the result in another array.
eg.,
String[] a={1,2,3,3};
String []b;
b should be 1.2.3only........not 1,2,3,3
thanks in advance,
regards,
aswath.n.s
(knowledge is power)


What is your question?!

regards,
sukatoa

Reply
r035198x's Avatar
r035198x
Administrator
10,647 Posts
March 26th, 2008
01:50 PM
#3

Re: delete replicating values from an array[]....
Quote:
Originally Posted by aswath
hi dere,
i have a question.
i have an array with replicating values.... al i want to do is
delete all replicating values and store the result in another array.
eg.,
String[] a={1,2,3,3};
String []b;
b should be 1.2.3only........not 1,2,3,3
thanks in advance,
regards,
aswath.n.s
(knowledge is power)


... and what have you done so far?

Kind regards

HashSet.

Reply
JosAH's Avatar
JosAH
Chief Editor
7,526 Posts
March 26th, 2008
03:56 PM
#4

Re: delete replicating values from an array[]....
Quote:
Originally Posted by aswath
(knowledge is power)


I don't think so: knowing what to do with your knowledge may give some power,
otherwise you just have to carry that knowledge around in your brain.

kind regards,

Jos

Reply
r035198x's Avatar
r035198x
Administrator
10,647 Posts
March 26th, 2008
04:25 PM
#5

Re: delete replicating values from an array[]....
Quote:
Originally Posted by JosAH
I don't think so: knowing what to do with your knowledge may give some power,
otherwise you just have to carry that knowledge around in your brain.

kind regards,

Jos


... and to prove that, I wrote some VB.NET code today.(there's a first time for everything).
@Jos surely there's an easier way for that problem?

Reply
JosAH's Avatar
JosAH
Chief Editor
7,526 Posts
March 26th, 2008
05:10 PM
#6

Re: delete replicating values from an array[]....
Quote:
Originally Posted by r035198x
... and to prove that, I wrote some VB.NET code today.(there's a first time for everything).
@Jos surely there's an easier way for that problem?


Erm, I didn't mean to say that when you accidentally know something that you
*have* to exercise that knowledge. Some knowledge can better be forgotten ;-)

kind regards,

Jos

ps. you can also iterate from the right and stop when you hit a letter ...

Reply
r035198x's Avatar
r035198x
Administrator
10,647 Posts
March 26th, 2008
05:18 PM
#7

Re: delete replicating values from an array[]....
Quote:
Originally Posted by JosAH
...

ps. you can also iterate from the right and stop when you hit a letter ...


Yeah I knew that, I just couldn't grasp the syntax for the for loop!!

*hides in a little corner*

Reply
JosAH's Avatar
JosAH
Chief Editor
7,526 Posts
March 26th, 2008
05:29 PM
#8

Re: delete replicating values from an array[]....
Quote:
Originally Posted by r035198x
Yeah I knew that, I just couldn't grasp the syntax for the for loop!!


I consider that a little plus on your side.

Quote:
Originally Posted by r035198x
*hides in a little corner*


No need to, nobody is perfect. Case closed, this is a Java forum after all.

kind regards,

Jos ( <--- knows nooooothing about Basic ;-)

Reply
r035198x's Avatar
r035198x
Administrator
10,647 Posts
March 26th, 2008
06:11 PM
#9

Re: delete replicating values from an array[]....
Quote:
Originally Posted by JosAH
I consider that a little plus on your side.



No need to, nobody is perfect. Case closed, this is a Java forum after all.

kind regards,

Jos ( <--- knows nooooothing about Basic ;-)


Coming back to the original question then, how would I get this duplicates Snipper to work for primitives as well?


Code: ( text )
  1. import java.util.Arrays;
  2. import java.util.Collection;
  3. import java.util.HashSet;
  4. public class  Snipper<E> {
  5.     E[] array;
  6.     public static void main(String[] args) {
  7.         Integer[] x = {4,4,7};
  8.         System.out.println(Arrays.toString(new Snipper<Integer>().removeDups(x)));
  9.     }
  10.     public E[] removeDups(Object o) {
  11.         Collection<E> c = null;
  12.        
  13.         if(o instanceof Collection) {
  14.             c = (Collection<E>)o;
  15.         }
  16.         else {
  17.             try {
  18.                 array = (E[])o;
  19.             }
  20.             catch(ClassCastException cCE) {
  21.                 System.out.println("Not supported");
  22.                
  23.             }
  24.         }
  25.         if(c != null) {
  26.             HashSet<E> s = new HashSet (c);
  27.             array = (E[])s.toArray();
  28.         }
  29.         else if(array != null) {
  30.             final HashSet<E> s = new HashSet () {
  31.                 {
  32.                     for(E e : array) {
  33.                         add(e);
  34.                     }
  35.                 }
  36.             };
  37.             array = (E[])s.toArray();
  38.         }
  39.         return array;
  40.     }
  41. }

Reply
lopez's Avatar
lopez
Newbie
6 Posts
March 27th, 2008
06:42 AM
#10

Re: delete replicating values from an array[]....
Quote:
Originally Posted by r035198x
Coming back to the original question then, how would I get this duplicates Snipper to work for primitives as well?


Code: ( text )
  1. import java.util.Arrays;
  2. import java.util.Collection;
  3. import java.util.HashSet;
  4. public class  Snipper<E> {
  5.     E[] array;
  6.     public static void main(String[] args) {
  7.         Integer[] x = {4,4,7};
  8.         System.out.println(Arrays.toString(new Snipper<Integer>().removeDups(x)));
  9.     }
  10.     public E[] removeDups(Object o) {
  11.         Collection<E> c = null;
  12.        
  13.         if(o instanceof Collection) {
  14.             c = (Collection<E>)o;
  15.         }
  16.         else {
  17.             try {
  18.                 array = (E[])o;
  19.             }
  20.             catch(ClassCastException cCE) {
  21.                 System.out.println("Not supported");
  22.                
  23.             }
  24.         }
  25.         if(c != null) {
  26.             HashSet<E> s = new HashSet (c);
  27.             array = (E[])s.toArray();
  28.         }
  29.         else if(array != null) {
  30.             final HashSet<E> s = new HashSet () {
  31.                 {
  32.                     for(E e : array) {
  33.                         add(e);
  34.                     }
  35.                 }
  36.             };
  37.             array = (E[])s.toArray();
  38.         }
  39.         return array;
  40.     }
  41. }




come on frds.....
i said knowledge is power not MY knowledge is powerful...
n'way lets drop this issue...
hi r035198x ,
i used ur code and its working fine.....
however i would relly appricate if u could explain me that... i hope i'm not bothering u...
regards,
aswath ns
(knowledge is power)

Reply
r035198x's Avatar
r035198x
Administrator
10,647 Posts
March 27th, 2008
07:10 AM
#11

Re: delete replicating values from an array[]....
Quote:
Originally Posted by lopez
come on frds.....
i said knowledge is power not MY knowledge is powerful...
n'way lets drop this issue...
hi r035198x ,
i used ur code and its working fine.....
however i would relly appricate if u could explain me that... i hope i'm not bothering u...
regards,
aswath ns
(knowledge is power)

I didn't think you were going to use it because I thought you had an int[].
Which part do you not understand?

Reply
r035198x's Avatar
r035198x
Administrator
10,647 Posts
March 27th, 2008
07:14 AM
#12

Re: delete replicating values from an array[]....
Hey, are you using two accounts?

Reply
aswath's Avatar
aswath
Newbie
18 Posts
March 28th, 2008
04:04 AM
#13

Re: delete replicating values from an array[]....
Quote:
Originally Posted by r035198x
Hey, are you using two accounts?



she is my college Colleague.. check her mail id if needed frnd.. i posed from her account...
i cannot get u wen u say<E>.. is that some generic terms r wat.. can u plz help me with that.....
regards

Reply
r035198x's Avatar
r035198x
Administrator
10,647 Posts
March 28th, 2008
06:25 AM
#14

Re: delete replicating values from an array[]....
Quote:
Originally Posted by aswath
she is my college Colleague.. check her mail id if needed frnd.. i posed from her account...
i cannot get u wen u say<E>.. is that some generic terms r wat.. can u plz help me with that.....
regards

You can read all about those here.

Reply
prisesh26's Avatar
prisesh26
Newbie
29 Posts
March 28th, 2008
09:16 AM
#15

Re: delete replicating values from an array[]....
wat happened to this forum..
why the Admin's are very hard in their words.

please understand the other people those who are starter, freshers to this language and selected this forum for help.

why cant the experienced people know and understand this and reply to your
help needed people.

no one knows fully and no one is unknown here...

please dont use hard words which will make the subscribers not to visit again


this forum helped me so much..

so i wrote this reply.

Even though this is not the JAVA RELATED REPLY.


regards!!!
well wisher

Reply
r035198x's Avatar
r035198x
Administrator
10,647 Posts
March 28th, 2008
09:27 AM
#16

Re: delete replicating values from an array[]....
Quote:
Originally Posted by prisesh26
wat happened to this forum..
why the Admin's are very hard in their words.

please understand the other people those who are starter, freshers to this language and selected this forum for help.

why cant the experienced people know and understand this and reply to your
help needed people.

no one knows fully and no one is unknown here...

please dont use hard words which will make the subscribers not to visit again


this forum helped me so much..

so i wrote this reply.

Even though this is not the JAVA RELATED REPLY.


regards!!!
well wisher


Feedback related messages should be posted in the feedback forum only. If you feel there is an inappropriate response from an Admin (or moderator) you can post your feelings in that forum, stating the inappropriate response of course.

Reply
prisesh26's Avatar
prisesh26
Newbie
29 Posts
March 28th, 2008
09:45 AM
#17

Re: delete replicating values from an array[]....
but it will take time to go through the feed back section in their busy schedule.
so i have written here. any way i will post it FEEDBACK section.

Reply
Reply
Not the answer you were looking for? Post your question . . .
184,042 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Java Forum Contributors