Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8315477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:08:23+00:00 2026-06-08T21:08:23+00:00

In Java, using the following function for a huge matrix X to print its

  • 0

In Java, using the following function for a huge matrix X to print its column-distinct elements:

// create the list of distinct values
List<Integer> values = new ArrayList<Integer>();

// X is n * m int[][] matrix
for (int j = 0, x; j < m; j++) {
    values.clear();
    for (int i = 0; i < n; i++) {
        x = X[i][j];
        if (values.contains(x)) continue;
        System.out.println(x);
        values.add(x);
    }
}

First I iterate by columns (index j) and inside by rows (index i).

This function will be called millions of times for different matrices, so the code should be optimized to meet the performance requirements. I’m wondering about the values array. Would it be faster to use values = new ArrayList<Integer>(); or values = null instead of values.clear() ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:08:24+00:00Added an answer on June 8, 2026 at 9:08 pm

    What would be much more efficient would be to use a Set instead of a list, for example the HashSet implementation. The contains method will run in O(1) instead of O(n) with a list. And you could save one call by only calling the add method.

    As for your specific question, I would just create a new Set at each loop – object creation is not that expensive, probably less than clearing the set (as confirmed by the benchmark at the bottom – see the most efficient version in EDIT 2):

    for (int j = 0, x; j < m; j++) {
        Set<Integer> values = new HashSet<Integer>();
        for (int i = 0; i < n; i++) {
            x = X[i][j];
            if (!values.add(x)) continue; //value.add returns true if the element was NOT in the set before
            System.out.println(x);
        }
    }
    

    However, the only way to know which is quicker (new object vs. clear) is to profile that portion of your code and check the performance of both versions.

    EDIT

    I ran a quick benchmark and the clear version seems a little faster than creating a set at each loop (by about 20%). You should still check on your dataset / use case which one is better. Faster code with my dataset:

    Set<Integer> values = new HashSet<Integer>();
    for (int j = 0, x; j < m; j++) {
        for (int i = 0; i < n; i++) {
            x = X[i][j];
            if (!values.add(x)) continue; //value.add returns true if the element was NOT in the set before
            System.out.println(x);
        }
        values.clear();
    }
    

    EDIT 2

    An actually even faster version of the code is obtained by creating a new set of the right size at each loop:

    for (int j = 0, x; j < m; j++) {
        Set<Integer> values = new HashSet<Integer>(n, 1); //right size from the beginning
        for (int i = 0; i < n; i++) {
            x = X[i][j];
            if (!values.add(x)) continue; //value.add returns true if the element was NOT in the set before
            System.out.println(x);
        }
    }
    

    Summary of result

    After JVM warm up + JIT:

    Set<Integer> values = new HashSet<Integer>(n, 1); =====> 280 ms
    values.clear();                                   =====> 380 ms
    Set<Integer> values = new HashSet<Integer>();     =====> 450 ms 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the BitmapFactory.decodeStream function, I've got the following exception 02-22 13:24:34.129: W/System.err(7927): java.io.FileNotFoundException: toto.jpg
I am trying to run the following C function from Java using JNA, but
i am using org.w3c.dom in java to read an xml with the following function
I am trying to execute windows commands from Java using the following code, Process
In an app we are calculating a SHA1Hmac in java using the following: SecretKey
I am trying to download a file using simple java class using the following
I am trying to download a file using simple java class using the following
I am using following code in a Java Agent in Lotus Notes: Session session
I tried using the following code in .java(main activity): final ImageView diskView1 = (ImageView)
I am trying to post some java code snippets on wordpress.com using the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.