10 Replies Latest reply: Feb 8, 2011 7:47 AM by sabre150 RSS

    Get int array of unique values out of original int array

    836518
      Hi Pros,

      I am new to Java. Below is interview question asked to me in one of the interviews.
      int[] intArray = {1, 2, -10, 3, 5, 8, 2, -10, 3};
      
      You have a method which returns int[] array  of unique values from above array. i.e 2, -10, 3 should appear only once.
      
      Here is the signature of the method, Please fill in the code ....
      
      <some access specifier> int[]  getUniqueValuesArray(int[] intArray){
      
      
         ---------  Write code here ------------------------
      
      
      }
      I wrote the code with the help of Set, but the interviewer said he wants implementation without using Set.

      Any Idea ?

      Thanks a Lot
      Abhijit

      Edited by: 833515 on Feb 5, 2011 7:49 AM
        • 1. Re: Get int array of unique values out of original int array
          Kayaman
          833515 wrote:
          Any Idea ?
          Stop going to interviews if you're not competent enough?

          Otherwise, it's a simple logic problem. How would you do it by hand?
          A good programmer doesn't simply rely on ready made classes like Set, but is a master in logic and other computer related things.

          Edited by: Kayaman on Feb 5, 2011 5:24 PM
          • 2. Re: Get int array of unique values out of original int array
            836518
            Yes, its true that its a simple logic problem.

            But the interview was especially for Java developer. So I thought he is expecting me to use available APIs.

            Why to re-invent the wheel if the APIs are already providing the logic.....

            here I am looking for if there are any APIs that we can use.

            Edited by: 833515 on Feb 5, 2011 7:44 AM
            • 3. Re: Get int array of unique values out of original int array
              sabre150
              I can find no way to do it without using an explicit loop. It's a slow day so I did it three ways. Since it does not use a Set, my preferred solution is :-
               static private int[] getUniqueValuesArray(int[] intArray)
                  {
                      final int[] result = intArray.clone();
                      Arrays.sort(result);
                      int j = 0;
                      for (int i = 1; i < intArray.length; i++)
                      {
                          if (result[j] != result)
              {
              result[++j] = result[i];
              }
              }
              return Arrays.copyOf(result, ++j);
              }


              Of course this may not be the fastest solution but I like it.

              Since the problem is trivial, if I was asked this in a Java interview I would be very concerned about the standard of developers they expected to employ and would be very worried about joining them.
              • 4. Re: Get int array of unique values out of original int array
                836518
                Thanks for your input sabre150. You are the man.
                • 5. Re: Get int array of unique values out of original int array
                  796440
                  833515 wrote:
                  Yes, its true that its a simple logic problem.

                  But the interview was especially for Java developer. So I thought he is expecting me to use available APIs.

                  Why to re-invent the wheel if the APIs are already providing the logic.....
                  On the job, you would of course use Set, but you should also possess the logic skills necessary to solve the problem yourself. The reasoning being, if you can't solve this simple problem, you're not going to be able to solve the real problems that come up as part of your job.
                  here I am looking for if there are any APIs that we can use.
                  If you weren't allowed to use Set, why would you be allowed to use some other canned API if it exists?

                  Again, your first step, in this and every problem you encounter when programming, is to figure out how you would do it "by hand". That is, come up with an algorithm that is independent of any programming language or library.
                  • 6. Re: Get int array of unique values out of original int array
                    796440
                    833515 wrote:
                    Thanks for your input sabre150. You are the man.
                    So, that solution didn't pop into your head the minute the interviewer told you not to use Set?

                    Yeesh. Scary.
                    • 7. Re: Get int array of unique values out of original int array
                      sabre150
                      Jizz wrote:
                      Thanks for your input sabre150. You are the man.
                      Yet nobody has pointed out the bug! To deal with a zero length input it should return :-
                             return (intArray.length == 0) ? intArray : Arrays.copyOf(result, ++j);
                      P.S. If you were conning us and this is actually homework then you should be aware that most colleges and universities keep an active watch on this site and others like it.
                      • 8. Re: Get int array of unique values out of original int array
                        YoungWinston
                        Jizz wrote:
                        But the interview was especially for Java developer. So I thought he is expecting me to use available APIs.
                        So would I, and I'd probably have given a similar solution to yours. However, you now know the rules and should write your code accordingly.

                        Before you start, you might want to ask whether you're also forbidden from using 'Arrays' though.

                        Winston
                        • 9. Re: Get int array of unique values out of original int array
                          tschodt
                          sabre150 wrote:
                          Arrays.sort(result);
                          Not sure about re-ordering the elements for no apparent reason.
                          >
                          Since the problem is trivial, if I was asked this in a Java interview
                          I would be very concerned about the standard of developers they expected to employ and would be very worried about joining them.
                          Maybe they just want to see what weird and wonderful alternatives you can think of
                          class J&#8203;izz {
                              static final int[] intArray = {1, 2, -10, 3, 5, 8, 2, -10, 3};
                          
                          /* 
                          You have a method which returns int[] array  of unique values from above array. i.e 2, -10, 3 should appear only once.
                           
                          Here is the signature of the method, Please fill in the code ....
                          */
                          
                              public int[] getUniqueValuesArray(int[] intArray) {
                                  // a List implementation that adds elements only if they are not already contained
                                  UniqueList<Integer> list = new UniqueList<Integer>() {
                                      @Override public boolean add(Integer o) { return contains(o)?false:super.add(o); }
                                      @Override public boolean addAll(Object o) { int[] ia=(int[])o; boolean r=false; for(int i:ia) r|=add(i); return r; }
                                      @Override public Object toArray(Object o) { int[] ia=(int[])o; if(ia.length<size()) ia=new int[size()]; int x=0; for(Integer v:toArray(new Integer[0])) ia[x++]=v; return ia; }
                                  };
                                  list.addAll((Object)intArray);
                                  return (int[])list.toArray(new int[0]);
                              }
                          
                              public abstract class UniqueList<E> extends java.util.ArrayList<E> // throws ClassCastException
                              {
                                  abstract boolean addAll(Object o);
                                  abstract Object toArray(Object o);
                              }
                          
                              public static final void main(final String[] arg) {
                                  System.out.println(java.util.Arrays.toString(new J&#8203;izz().getUniqueValuesArray(intArray)));
                              }
                          }
                          By the way, the forum seems to censor the word 'J&#8203;izz' - maybe that username is breaking the ToU?
                          • 10. Re: Get int array of unique values out of original int array
                            sabre150
                            tschodt wrote:
                            sabre150 wrote:
                            Arrays.sort(result);
                            Not sure about re-ordering the elements for no apparent reason.
                            My solution only works if the array is sorted.