Help with solution for "MISSP"

hello, i came up with a solution for the problem “MISSP” but instead of returning the actual integer in the array, it returns the index location of the array.

so for this task, we are supposed to return the integer without a pair, and i’ve come up with an algorithm that’ll count occurrences of each number and return the one that has an occurrence of only 1.

however, it’s returning the index location and i can’t seem to get it to return the actual integer itself…

specifically, it’s the “return j” line and i’d imagine it to be “return arr[j]” but it’s returning 0 when i type that

	public static int chef_and_dolls(int[] nums, int size) {
		int[] arr = new int[size];
		
		for(int i=0; i<arr.length; i++)
			arr[nums[i]-1]++;
		
		for(int j=0; j<arr.length; j++) {
			if(arr[nums[j]-1] == 1)
				return j;
		}
		
		return 0;
	}
	
	public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int arSize = scan.nextInt();
        int[] ar = new int[arSize];
        for(int i=0; i<n; i++) {
        	for(int j=0; j<arSize; j++) {
        		ar[j] = scan.nextInt();
        	}
        }
        System.out.println(chef_and_dolls(ar, arSize));
        scan.close();
    }

If you want it to return the “actual integer itself”, why would you return an element of the array containing the counts of each integer?

return num[j]; sounds closer to what you want.

Edit:

Though that won’t be enough to fix it fully. I think your confusing naming scheme is making it much harder for you to spot the errors.

Below, I’ve taken your original code and left it completely unchanged, except for adding the stuff necessary to make it compile and renaming some things so that they actually represent what they mean. See if that makes it easier to spot the errors! :slight_smile:

import java.util.Scanner;

class ChefAndDolls {
    public static int findTypeOfMissingDoll(int[] typeOfDoll, int numDolls) {
		int[] numDollsWithType = new int[numDolls];

		for(int i=0; i<numDollsWithType.length; i++)
			numDollsWithType[typeOfDoll[i]-1]++;

		for(int j=0; j<numDollsWithType.length; j++) {
			if(numDollsWithType[typeOfDoll[j]-1] == 1)
				return j;
		}

		return 0;
	}

	public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int numTestCases = scan.nextInt();
        int numDolls = scan.nextInt();
        int[] typeOfDoll = new int[numDolls];
        for(int t=0; t<numTestCases; t++) {
        	for(int dollIndex=0; dollIndex<numDolls; dollIndex++) {
        		typeOfDoll[dollIndex] = scan.nextInt();
        	}
        }
        System.out.println(findTypeOfMissingDoll(typeOfDoll, numDolls));
        scan.close();
    }
}
1 Like

i see, i got caught up in similar looking variables and got confused which held which for a sec there, i even forgot i was working with the nums[] for a second!

thank you so much for the feedback and criticism! i’ll work on making better names with future functions

1 Like