//Apex class to generate the list of user assigned to a profile
List<User> lUser = [SELECT Id , ProfileId from User ]; // list of users
List<Profile> lProfile = [SELECT Id, ProfileID from Profile];
List<Profile> noUserProfile = [SELECT id, ProfileId from Profile];
for(Profile p : lProfile)
{
//iterate and figure out fill up the corresponding list
bool flag= false;
for(User u : lUser)
{
if(profileId ==p.id)
flag=true;
}
if(!flag) //whenever the flag is true
noUserProfile.add(p); //add this profile to nouserprofile list
} //end of iteration for profile list
System.debug(\'profileWithNoUser ::: \' + profileWithNoUser + \' ::: \' + profileWithNoUser.size() ) ;
}