Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I guess it's quite good in 22 minutes.

1.

Hashtable<Integer, Integer> myTable = new Hashtable<Integer, Integer>();

Instead of the Hashtable use HashMap, but you use it like a Set (same key and value, so value is unused), so use a HashSet instead. See: Differences between HashMap and Hashtable? Differences between HashMap and Hashtable?

  1. I would use a longer variable names than A. Longer names would make the code more readable since readers don't have to decode the abbreviations every time and when they write/maintain the code don't have to guess which abbreviation the author uses. Furthermore, usual Java variable names are camelCase, with lowercase first letter. (See: Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions)

  2. myTable could have a more descriptive name.

  3. I've removed everything which does not change minVal's value:

int minVal=0;
...
for(int i : A){
 if(i<minVal && i>0){
 minVal = i;
 }
 ...
}

If I'm right the condition of the if statement is always false. If you substitute minVal's initial value, you get this:

i < 0 && i > 0

which is false, so minVal's value never can be changed.

  1. I'd put a guard clause inside the for to get rid of the negative values and save some memory in the set:

     for (int i: A) {
     if (i < 0) {
     continue;
     }
     set.add(i);
     }
    
  1. If you want to save some memory with big, non-repetitive input arrays you could use a new BitSet(A.length).

I guess it's quite good in 22 minutes.

1.

Hashtable<Integer, Integer> myTable = new Hashtable<Integer, Integer>();

Instead of the Hashtable use HashMap, but you use it like a Set (same key and value, so value is unused), so use a HashSet instead. See: Differences between HashMap and Hashtable?

  1. I would use a longer variable names than A. Longer names would make the code more readable since readers don't have to decode the abbreviations every time and when they write/maintain the code don't have to guess which abbreviation the author uses. Furthermore, usual Java variable names are camelCase, with lowercase first letter. (See: Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions)

  2. myTable could have a more descriptive name.

  3. I've removed everything which does not change minVal's value:

int minVal=0;
...
for(int i : A){
 if(i<minVal && i>0){
 minVal = i;
 }
 ...
}

If I'm right the condition of the if statement is always false. If you substitute minVal's initial value, you get this:

i < 0 && i > 0

which is false, so minVal's value never can be changed.

  1. I'd put a guard clause inside the for to get rid of the negative values and save some memory in the set:

     for (int i: A) {
     if (i < 0) {
     continue;
     }
     set.add(i);
     }
    
  1. If you want to save some memory with big, non-repetitive input arrays you could use a new BitSet(A.length).

I guess it's quite good in 22 minutes.

1.

Hashtable<Integer, Integer> myTable = new Hashtable<Integer, Integer>();

Instead of the Hashtable use HashMap, but you use it like a Set (same key and value, so value is unused), so use a HashSet instead. See: Differences between HashMap and Hashtable?

  1. I would use a longer variable names than A. Longer names would make the code more readable since readers don't have to decode the abbreviations every time and when they write/maintain the code don't have to guess which abbreviation the author uses. Furthermore, usual Java variable names are camelCase, with lowercase first letter. (See: Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions)

  2. myTable could have a more descriptive name.

  3. I've removed everything which does not change minVal's value:

int minVal=0;
...
for(int i : A){
 if(i<minVal && i>0){
 minVal = i;
 }
 ...
}

If I'm right the condition of the if statement is always false. If you substitute minVal's initial value, you get this:

i < 0 && i > 0

which is false, so minVal's value never can be changed.

  1. I'd put a guard clause inside the for to get rid of the negative values and save some memory in the set:

     for (int i: A) {
     if (i < 0) {
     continue;
     }
     set.add(i);
     }
    
  1. If you want to save some memory with big, non-repetitive input arrays you could use a new BitSet(A.length).
added 242 characters in body
Source Link
palacsint
  • 30.3k
  • 9
  • 82
  • 157

    I guess it's quite good in 22 minutes.

    1.

    Hashtable<Integer, Integer> myTable = new Hashtable<Integer, Integer>();
    

    Instead of the Hashtable use HashMap, but you use it like a Set (same key and value, so value is unused), so use a HashSet instead. See: Differences between HashMap and Hashtable?

    1. I would use a longer variable names than A. Longer names would make the code more readable since readers don't have to decode the abbreviations every time and when they write/maintain the code don't have to guess which abbreviation the author uses. Furthermore, usual Java variable names are camelCase, with lowercase first letter. (See: Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions)

    2. myTable could have a more descriptive name.

    3. I've removed everything which does not change minVal's value:

    int minVal=0;
    ...
    for(int i : A){
     if(i<minVal && i>0){
     minVal = i;
     }
     ...
    }
    

    If I'm right the condition of the if statement is always false. If you substitute minVal's initial value, you get this:

    i < 0 && i > 0
    

    which is false, so minVal's value never can be changed.

    1. I'd put a guard clause inside the for to get rid of the negative values and save some memory in the set:

       for (int i: A) {
       if (i < 0) {
       continue;
       }
       set.add(i);
       }
      
    1. If you want to save some memory with big, non-repetitive input arrays you could use a new BitSet(A.length) .
      Hashtable<Integer, Integer> myTable = new Hashtable<Integer, Integer>();
      

      Instead of the Hashtable use HashMap, but you use it like a Set (same key and value, so value is unused), so use a HashSet instead. See: Differences between HashMap and Hashtable?

      1. I would use a longer variable names than A. Longer names would make the code more readable since readers don't have to decode the abbreviations every time and when they write/maintain the code don't have to guess which abbreviation the author uses. Furthermore, usual Java variable names are camelCase, with lowercase first letter. (See: Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions)

      2. myTable could have a more descriptive name.

      3. I've removed everything which does not change minVal's value:

      int minVal=0;
      ...
      for(int i : A){
       if(i<minVal && i>0){
       minVal = i;
       }
       ...
      }
      

      If I'm right the condition of the if statement is always false. If you substitute minVal's initial value, you get this:

      i < 0 && i > 0
      

      which is false, so minVal's value never can be changed.

      1. I'd put a guard clause inside the for to get rid of the negative values and save some memory in the set:

         for (int i: A) {
         if (i < 0) {
         continue;
         }
         set.add(i);
         }
        

      I guess it's quite good in 22 minutes.

      1.

      Hashtable<Integer, Integer> myTable = new Hashtable<Integer, Integer>();
      

      Instead of the Hashtable use HashMap, but you use it like a Set (same key and value, so value is unused), so use a HashSet instead. See: Differences between HashMap and Hashtable?

      1. I would use a longer variable names than A. Longer names would make the code more readable since readers don't have to decode the abbreviations every time and when they write/maintain the code don't have to guess which abbreviation the author uses. Furthermore, usual Java variable names are camelCase, with lowercase first letter. (See: Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions)

      2. myTable could have a more descriptive name.

      3. I've removed everything which does not change minVal's value:

      int minVal=0;
      ...
      for(int i : A){
       if(i<minVal && i>0){
       minVal = i;
       }
       ...
      }
      

      If I'm right the condition of the if statement is always false. If you substitute minVal's initial value, you get this:

      i < 0 && i > 0
      

      which is false, so minVal's value never can be changed.

      1. I'd put a guard clause inside the for to get rid of the negative values and save some memory in the set:

         for (int i: A) {
         if (i < 0) {
         continue;
         }
         set.add(i);
         }
        
      1. If you want to save some memory with big, non-repetitive input arrays you could use a new BitSet(A.length) .
      Post Undeleted by palacsint
      added 779 characters in body
      Source Link
      palacsint
      • 30.3k
      • 9
      • 82
      • 157
      Hashtable<Integer, Integer> myTable = new Hashtable<Integer, Integer>();
      

      Instead of the Hashtable use HashMap, but you use it like a Set (same key and value, so value is unused), so use a HashSet instead. See: Differences between HashMap and Hashtable?

      1. I would use a longer variable names than A. Longer names would make the code more readable since readers don't have to decode the abbreviations every time and when they write/maintain the code don't have to guess which abbreviation the author uses. Furthermore, usual Java variable names are camelCase, with lowercase first letter. (See: Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions)

      2. myTable could have a more descriptive name.

      3. I've removed everything which does not change minVal's value:

      int minVal=0;
      ...
      for(int i : A){
       if(i<minVal && i>0){
       minVal = i;
       }
       ...
      }
      

      If I'm right the condition of the if statement is always false. If you substitute minVal's initial value, you get this:

      i < 0 && i > 0
      

      which is false, so minVal's value never can be changed.

      1. I'd put a guard clause inside the for to get rid of the negative values and save some memory in the set:

         for (int i: A) {
         if (i < 0) {
         continue;
         }
         set.add(i);
         }
        
      Hashtable<Integer, Integer> myTable = new Hashtable<Integer, Integer>();
      

      Instead of the Hashtable use HashMap, but you use it like a Set (same key and value, so value is unused), so use a HashSet instead. See: Differences between HashMap and Hashtable?

      1. I would use a longer variable names than A. Longer names would make the code more readable since readers don't have to decode the abbreviations every time and when they write/maintain the code don't have to guess which abbreviation the author uses. Furthermore, usual Java variable names are camelCase, with lowercase first letter. (See: Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions)

      2. myTable could have a more descriptive name.

      Hashtable<Integer, Integer> myTable = new Hashtable<Integer, Integer>();
      

      Instead of the Hashtable use HashMap, but you use it like a Set (same key and value, so value is unused), so use a HashSet instead. See: Differences between HashMap and Hashtable?

      1. I would use a longer variable names than A. Longer names would make the code more readable since readers don't have to decode the abbreviations every time and when they write/maintain the code don't have to guess which abbreviation the author uses. Furthermore, usual Java variable names are camelCase, with lowercase first letter. (See: Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions)

      2. myTable could have a more descriptive name.

      3. I've removed everything which does not change minVal's value:

      int minVal=0;
      ...
      for(int i : A){
       if(i<minVal && i>0){
       minVal = i;
       }
       ...
      }
      

      If I'm right the condition of the if statement is always false. If you substitute minVal's initial value, you get this:

      i < 0 && i > 0
      

      which is false, so minVal's value never can be changed.

      1. I'd put a guard clause inside the for to get rid of the negative values and save some memory in the set:

         for (int i: A) {
         if (i < 0) {
         continue;
         }
         set.add(i);
         }
        
      Post Deleted by palacsint
      Post Undeleted by palacsint
      added 148 characters in body
      Source Link
      palacsint
      • 30.3k
      • 9
      • 82
      • 157
      Loading
      Post Deleted by palacsint
      Source Link
      palacsint
      • 30.3k
      • 9
      • 82
      • 157
      Loading
      lang-java

      AltStyle によって変換されたページ (->オリジナル) /