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 7081223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:53:35+00:00 2026-05-28T06:53:35+00:00

The problem is the following. There are multiple rows that have non-unique identifiers: id

  • 0

The problem is the following. There are multiple rows that have non-unique identifiers:

id value
0: {1,2,3}
0: {1,2,2}

1: {1,2,3}

2: {1,2,3}
2: {1,1,3}

I have the function equals that can compare multiple rows between each other. I need to write a code that selects the rows as an input of the function equals. The rows selected must have unique ids, BUT I should check all possible combinations of unique ids. For instance, if there are 5 rows with ids: 0,0,1,2,3, then I should check the following two combinations of ids: 0,1,2,3 and 0,1,2,3, because 0 apears twice. Of course, each of these two combinations will consist of unique rows that have id=0.

My code snippet is the following:

public class Test {
public static void main(String[] args) {
  ArrayList<Row> allRows = new ArrayList<Row>();
  allRows.add(new Row(0,new int[]{1,2,3}));
  allRows.add(new Row(0,new int[]{1,2,2}));
  allRows.add(new Row(1,new int[]{1,2,3}));
  allRows.add(new Row(2,new int[]{1,2,3}));
  allRows.add(new Row(2,new int[]{1,1,3}));

  boolean answer = hasEqualUniqueRows(allRows);
}

private boolean hasEqualUniqueRows(ArrayList<Row> allTokens) {
    for (int i=0; i<allTokens.size(); i++) {
        ArrayList<Integer[]> rows = new ArrayList<Integer[]>();
        rows = findUniqueRows(i,allTokens);
        boolean answer = equalsExceptForNulls(rows);
        if (answer) return true;
    }
    return false;
}
// Compare rows for similarities
public static <T> boolean equalsExceptForNulls(ArrayList<T[]> ts) {
    for (int i=0; i<ts.size(); i++) {
        for (int j=0; j<ts.size(); j++) {
            if (i != j) {
                boolean answer = equals(ts.get(i),ts.get(j));
                if (!answer) return false;
            }
        }
    }
    return true;
}

public static <T> boolean equals(T[] ts1, T[] ts2) {
    if (ts1.length != ts2.length) return false;
    for(int i = 0; i < ts1.length; i++) {
       T t1 = ts1[i], t2 = ts2[i];
       if (t1 != null && t2 != null && !t1.equals(t2)) 
           return false;
    }
    return true;
}

class Row {
          private String key;
          private Integer[] values;

      public Row(String k,Integer[] v) {
          this.key = k;
          this.values = v;
      }

      public String getKey() {
          return this.key;
      }

      public Integer[] getValues() {
          return this.values;
      }
}

}

Since the number of rows with unique ids is apriori unknown, I don´t know how to solve this problem. Any suggestions? Thanks.

Edit#1
I updated the code. Now it´s more complete. But it lacks the implementation of the function findUniqueRows. This function should select rows from the ArrayList that have unique keys (ids). Could someone help me to develop this function? Thanks.

  • 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-05-28T06:53:36+00:00Added an answer on May 28, 2026 at 6:53 am

    Assuming the objective is to find every combination without duplicates you can do this with the following. The test to find duplicates is just to confirm it doesn’t generate any duplicates in the first place.

    import java.util.*;
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class Main {
        public static void main(String... args) {
            Bag<Integer> b = new Bag<>();
            b.countFor(1, 2);
            b.countFor(2, 1);
            b.countFor(3, 3);
            Set<String> set = new LinkedHashSet<>();
            for (List<Integer> list : b.combinations()) {
                System.out.println(list);
                String s = list.toString();
                if (!set.add(s))
                    System.err.println("Duplicate entry " + s);
            }
        }
    }
    
    class Bag<E> {
        final Map<E, AtomicInteger> countMap = new LinkedHashMap<>();
    
        void countFor(E e, int n) {
            countMap.put(e, new AtomicInteger(n));
        }
    
        void decrement(E e) {
            AtomicInteger ai = countMap.get(e);
            if (ai.decrementAndGet() < 1)
                countMap.remove(e);
        }
    
        void increment(E e) {
            AtomicInteger ai = countMap.get(e);
            if (ai == null)
                countMap.put(e, new AtomicInteger(1));
            else
                ai.incrementAndGet();
        }
    
        List<List<E>> combinations() {
            List<List<E>> ret = new ArrayList<>();
            List<E> current = new ArrayList<>();
            combinations0(ret, current);
            return ret;
        }
    
        private void combinations0(List<List<E>> ret, List<E> current) {
            if (countMap.isEmpty()) {
                ret.add(new ArrayList<E>(current));
                return;
            }
            int position = current.size();
            current.add(null);
            List<E> es = new ArrayList<>(countMap.keySet());
            if (es.get(0) instanceof Comparable)
                Collections.sort((List) es);
            for (E e : es) {
                current.set(position, e);
                decrement(e);
                combinations0(ret, current);
                increment(e);
            }
            current.remove(position);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i Have a problem returning multiple rows in one output there are multiple user_roleID's
I have the following problem that I need to express. There are persons, workplaces
Avast there fellow programmers! I have the following problem: I have two rectangles overlapping
Propose to consider the following problem. Suppose we have some composite object. Are there
I need to solve the following problem: I have multiple rectangles of sizes: width
I have the following problem: I open multiple modal forms in a stack (for
I am trying to find an optimal solution for the following problem: there is
I've got following problem: (c#) There is some class (IRC bot), which has method,
is there an easy way to solve the following problem. Let's say I fetch
I had the following problem today, and I was wondering if there is a

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.