0

art[]=is a String array of artist names
plays[]=is an integer array of play-count for the respective artist

This data has been acquired from a huge dataset with different users. Hence,the art[] array contains duplicate artists. I need remove the duplicates and also add the play-count for the same artist received from different users.
eg-

art[beatles,floyd,beatles,dream theater,beatles,...]

may contain the respective play counts:

plays[100,200,50,30,200,....]


I need an array which will add all the play counts for the same artist and also remove duplicates. I tried the following but it doesn't work.

for(int i=0;i<9999;i++){
    for(int j=i+1;j<10000;j++){
/*  if(!artistplays[i][0].equals(null) && !artistplays[j][0].equals(null) && artistplays[i][0].equalsIgnoreCase(artistplays[j][0])){
artistplays[i][1]=String.valueOf((Integer.parseInt(artistplays[i][1]))+(Integer.parseInt(artistplays[j][1])));
            artistplays[j][0]=null;
            artistplays[j][1]=null;

        }*/ //I also tried implementing this in a 2D array where the 1st column is the artist and the 2nd column is for the respective play-count
if(!art[i].equalsIgnoreCase(null) && !art[j].equalsIgnoreCase(null) && art[i].equalsIgnoreCase(art[j])){
            plays[i]+=plays[j];
            art[j]=null;
            plays[j]=0;

        }
    }
}

When I run this,the arrays don't change. Where am I going wrong?

7
  • What happens when you run your code? What did you expect to happen? Commented Apr 18, 2014 at 17:56
  • The array does not change. It's the same as it was before. Both of them Commented Apr 18, 2014 at 17:59
  • 1
    this is bound to crash when art[i] is null. Use art[i] == null test instead. Commented Apr 18, 2014 at 18:01
  • Tried that right now. Doesn't work Commented Apr 18, 2014 at 18:05
  • Shreyos, yes I've already gone through that but what I'm trying to do cannot be implemented using HashSet Commented Apr 18, 2014 at 18:06

2 Answers 2

0

You could use a HashMap to loop through the data. Store the artist as key and the total play count as value.

While you loop through the original array check if is already in the new Map and store the count as value ( or add into it if already exist). If you really want it as an array, convert it into an array afterwards.

As is said in the comments the way you tried to do it will loop through the elements n^2 times, what is suboptimal. Your solution has O(n^2) complexity, if you use a HashMap you can get it in O(n).

Example:

public static void main(String[] args) {
    String art[] = {"beatles","floyd","beatles","dream", "theater","beatles"};
    int plays[]  = {100,       200,    50,      30,       200,      70};

    Map<String, Integer> artistCount = new HashMap<String, Integer>();

    for (int i = 0; i < art.length; i++) {
        if (!artistCount.containsKey(art[i])) {
            artistCount.put(art[i],0);
        }
        artistCount.put(art[i], artistCount.get(art[i]) + plays[i]);
    }

    String artFinal[] = new String[artistCount.size()];
    int playsFinal[] = new int[artistCount.size()];

    int i = 0;
    for (Map.Entry<String,Integer> en:artistCount.entrySet()) {
        artFinal[i] = en.getKey();
        playsFinal[i] = en.getValue();
        i++;
    }
    // to display results
    for (int j = 0; j < artFinal.length; j++) {
        System.out.println(artFinal[j]+": "+playsFinal[j]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

not to mention that this would be o(n) rather than o(n*n)
0

It's best solved using HashMap:

import java.util.LinkedHashMap;
import java.util.Map;

public class CountArtists {

    public static void main(String[] args) {
        String[] artists = { "beatles", "floyd", "beatles", "dream theater",
                "beatles" };

        Map<String, Integer> artistToCount = new LinkedHashMap<>();

        for (String artist : artists) {
            if (!artistToCount.containsKey(artist)) {
                artistToCount.put(artist, 1);
            } else {
                artistToCount.put(artist, artistToCount.get(artist) + 1);
            }
        }

        for (String artist : artistToCount.keySet()) {
            System.out.printf("%s => %d\n", artist, artistToCount.get(artist));
        }
    }
}

Plain array solution works in O(n^2) which can take pretty long, if array is huge as you say.

1 Comment

Thanks a lot but I guess you misunderstood my point. I do not want to count the occurrences of the artists. I already have the number of times that a user has played a particular artist in plays[]. Now both these arrays contain merged information from different users. In the example above I have 3 users who listen to beatles and also the number of times they have listen to beatles. I want to add the total number of times Beatles have been played by all the users combined

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.