Question

I made this code. I think it is wrong.

public void display() {
    for (int i = 0; i < tabT.length; i++)
        if (tabT[i] != null)
            for (int j = 0; j <= i; j++)
                if (tabT[i] != tabT[j])
                    System.out.println(tabT[i].getCar());
}

How do I display elements without redundancy in an array?

Was it helpful?

Solution

If you want to use only arrays, you can do something like this:

Make an temp (helper) array, which will include each element seen so far in tabT. Then, before printing the value, you check if it's not appearing in the helper array (tmp).

For example, if you have values in tabT, and you don't want to print each one more than once:

int[] tabT = {1,2,3,1,1,2,6,7,2,7,1};
int[] tmp = new int[tabT.length];
boolean flag;
for (int i = 0; i < tabT.length; i++) {
    tmp[i] = tabT[i];
    flag = true;
    for (int j = 0; j < tmp.length; j++)
        if (tabT[i] == tmp[j] && i!=j) {
            flag = false;
        }
    if(flag)
        System.out.println(tabT[i]);
}

Output: [1,2,3,6,7]

You can easily apply this idea to your program, and you'll have each element printed only once:

Cars[] tmp = new Cars[tabT.length]; //Assuming tabT is from type Cars[]
boolean flag = true;
for (int i = 0; i < tabT.length; i++) { 
    tmp[i] = tabT[i];
    if (tabT[i] != null) {
        for (int j = 0; j < tmp.length; j++)
            if (tabT[i].getCar().equals(tabT[j].getCar()) && i!=j)
                flag = false;
        if(flag)
            System.out.println(tabT[i].getCar());
    }
}

This will print each car (or whatever you're printing) only once.

OTHER TIPS

Objects compared via equals() For example

if (!tabT[i].equals(tabT[j]))

you are comparing the references values not the objects

for (int i=0; i< tabT.length; i++) {
  boolean f = false;
  for (int j=i+1; j <tabT.length; j++)
    if (tabT[i].equals(tabT[j])) {
      f=true;
      break;
    }
  if (!f)
    System.out.println(tabT[i].getCar());
}

this should give you all combinations non-repeating for i and j, so we don't compare them multiple times.

== and != test equality at the object level (i.e. if both instances are the same). What you need is to compare the value represented by each object (e.g. if two strings are equals), then you need to ask whether !tabT[i].equals(tabT[j]), and make the elements of tabT implement equals).

Or convert the array to a set, which removes duplicates.

T[] tabT = ...
Set<T> set = new LinkedHashSet<T>(Arrays.asList(tabT))
for (T t:set) System.out.println(t);

I used a LinkedHashSet because it preserves the order of the elements in the array. Note that you need to implement equals and hashcode.

Put tabT array in a Set. There will be no duplicate items.

Set tabTList = new HashMap(Listjava.util.Arrays.asList(tabT);

Why not trying something like this? (I'm supposing that you are working with String types)

HashSet<String> hashSet = new HashSet<String>();

for (int i = 0; i < tabT.length; i++) {
    hashSet.add(tabT[i]);
}

You can't have duplicates into a set, so now you can iterate the set to get the uniques.

java.util.Iterator<String> iterator = hashSet.iterator();

while (iterator.hasNext()) {
    System.out.println((String)iterator.next());
}

You could consider a HashMap if you do want to keep track of duplicate count. Iterate through array once to place objects into the HashMap with their respective counts. Then iterate through the array again checking against the HashMap. This would be O(n) time as opposed to potential O(n^2)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top