Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved my backend challenge #217

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added submissions/ornitcg/SongCounter.class
Binary file not shown.
83 changes: 83 additions & 0 deletions submissions/ornitcg/SongCounter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import java.io.*;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.HashMap;

public class SongCounter {

public static String mostCommon(HashMap<String,Integer> strMap){
int maxOccurence = 0;
String maxName = "";
for (Map.Entry<String, Integer> entry : strMap.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
// System.out.println("Key: " + key + ", Value: " + value);
if(value>maxOccurence){
maxOccurence= value;
maxName = key;
}
}
return maxName;
}






public static int findIndex(String str, String[] searchArray){
for (int i=0; i< searchArray.length ; i++){
if(searchArray[i].equals(str)){
return i;
}
}
return -1;
} //findIndex

public static void main(String[] args) {
String path = "./spotify-2023.csv";
try {
BufferedReader reader =new BufferedReader(new FileReader(path));
HashMap<String,Integer> artistOcc = new HashMap<String, Integer>();
String line = "";
line=reader.readLine();
String[] titles = line.trim().split(",");
int keyIndex= findIndex("key", titles);
int artistIndex= findIndex("artist(s)_name", titles);

int songsCounter = 0;
int eKeyCounter = 0;


while((line=reader.readLine())!=null){
songsCounter++;
String[] songData = line.trim().split(",");
if (songData[keyIndex].equals("E")){
eKeyCounter++;
}
String artist = songData[artistIndex];
if(artistOcc.containsKey(artist)){
artistOcc.put(artist, artistOcc.get(artist) + 1);
}
else{
artistOcc.put(artist, 1);

}

}
String mostCommonArtist = mostCommon(artistOcc);

System.out.println("Number of songs: "+ songsCounter);
System.out.println("Number of songs with E key: "+ eKeyCounter);
System.out.println("The artist with the higest occurence is: " + mostCommonArtist + " with " + artistOcc.get(mostCommonArtist) + " tracks");




}
catch(Exception e){
System.out.println("csv failed to load");

}
}
}
Loading