Advertisement
Iddon

Element Duplication Detection in Array Lists

Jan 21st, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. Element Duplication Detection in Array Lists
  2.    
  3.     Auth: Elliot Iddon
  4.     Vers: 0.1
  5.     Date: 21/1/2012
  6.  
  7.  
  8.     1. The Self Detection Problem
  9.     2. The Comparison Optimisation
  10.  
  11.  
  12.  
  13.     1. The Self Detection Problem
  14.         You need to make sure that you don't check the object you're searching
  15.         against for duplication. One way to do this is to add a check
  16.         for same object e.g.
  17.  
  18.             //In this example I will assume that c is the card you are
  19.             //searching against and d is your current possible duplicate.
  20.             if (..your code to check if the card has the same value and suit...
  21.             && !(c.equals(d)))
  22.        
  23.         However there are many more elegant solutions to this problem...
  24.  
  25.     2. The Comparison Optimisation
  26.         Incidentally a more elegant solution to The Self Detection Problem is
  27.         encompassed in an optimisation of which comparisons are performed.
  28.        
  29.         In a basic nested for loop you might take every element in an array:
  30.        
  31.          _ _ _ _ _ _ _
  32.          
  33.          and for each element compare it to every element in the same array:
  34.  
  35.              //In this diagram I'm assuming that none of the comparisons listed
  36.              are matching cards and therefore the loop proceeds
  37.  
  38.              * _ _ _ _ _ _ N
  39.  
  40.              X + _ _ _ _ _ R
  41.  
  42.              X _ + _ _ _ _
  43.  
  44.              X _ _ + _ _ _
  45.  
  46.              X _ _ _ + _ _
  47.  
  48.              X _ _ _ _ + _
  49.  
  50.              X _ _ _ _ _ +
  51.  
  52.              + X _ _ _ _ _ R
  53.  
  54.              _ * _ _ _ _ _ N
  55.  
  56.              _ X + _ _ _ _
  57.  
  58.              _ X _ + _ _ _
  59.  
  60.              _ X _ _ + _ _
  61.  
  62.              // here I got bored of writing them out
  63.  
  64.              ...
  65.  
  66.              _ _ _ _ _ _ * N
  67.              
  68.         So the problemed lines are lines marked with a letter.
  69.  
  70.             N indicates you should never be performing this comparison
  71.             obviously these cards will be the same but not duplicated!
  72.             R indicates this is a repeated comparison (I've just marked
  73.             one pair as I didn't draw the others).
  74.  
  75.         The solution to both of these is to compare only with elements AFTER the
  76.         element you are checking against.
  77.        
  78.         The structure of such a solution might be:
  79.            
  80.         public boolean hasDuplicate(ArrayList<ElementType> l){
  81.             for(int i = 0; i < l.size(); i++){
  82.                 ElementType e = l.get(i);
  83.                 for(int j = i+1; i < l.size(); j++){
  84.                     ElementType f = l.get(j);
  85.                     if(...some code to check if they are the same...)
  86.                         return true;
  87.                 }
  88.             }
  89.             return false;
  90.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement