# Matching algorithm

I have a list of pencils and a list of erasers. The goal it to check whether or not all the erasers can be put on pencils. An eraser may fit on multiple different pencils. Pencils can have at most 1 eraser.

If I just loop through all the erasers and put them on pencils, I end up with erasers that fit no unoccupied pencils even though there is a solution that has all the erasers on pencils.

What algorithm could I use to figure out a combination that fits all the erasers on pencils?

``````public class Eraser(){
public boolean matches(Pencil p){
//unimportant
}
}

public class Pencil(){
}
``````

My attempt

``````public boolean doMatch(List<Eraser> erasers, List<Pencil> pencils){
for (Eraser e : erasers) {
boolean found = false;
Iterator it = pencils.iterator();
while (it.hasNext()) {
Pencil p = (Pencil) it.next();
if (e.matches(p)) {
found = true;
it.remove();
break;
}
}
if (!found) {
return false;
}
}
return true;
}
``````

# Matching algorithm

I have a list of pencils and a list of erasers. The goal it to check whether or not all the erasers can be put on pencils. An eraser may fit on multiple different pencils. Pencils can have at most 1 eraser.

If I just loop through all the erasers and put them on pencils, I end up with erasers that fit no unoccupied pencils even though there is a solution that has all the erasers on pencils.

What algorithm could I use to figure out a combination that fits all the erasers on pencils?

``````public class Eraser(){
public boolean matches(Pencil p){
//unimportant
}
}

public class Pencil(){
}
``````

My attempt

``````public boolean doMatch(List<Eraser> erasers, List<Pencil> pencils){
for (Eraser e : erasers) {
boolean found = false;
Iterator it = pencils.iterator();
while (it.hasNext()) {
Pencil p = (Pencil) it.next();
if (e.matches(p)) {
found = true;
it.remove();
break;
}
}
if (!found) {
return false;
}
}
return true;
}
``````

# Get id of specific div

I need to display channel name when user do subscribe or unsubscribe. Below is two sub buttons and event script.

``````<script src="https://apis.google.com/js/platform.js"></script>

<div id="chan-1" class="g-ytsubscribe" data-channel="SMOSH" data-layout="default" data-count="default" data-onytevent="onYtEvent"></div>
</div>

<div id="chan-2" class="g-ytsubscribe" data-channel="Apple" data-layout="default" data-count="default" data-onytevent="onYtEvent"></div>
</div>

<script>

// Need to display subscribed channel name

} else if (payload.eventType == 'unsubscribe') {
// Need to display unsubscribed channel name

}
if (window.console) { // for debugging only
}
}
</script>
``````

# Get id of specific div

I need to display channel name when user do subscribe or unsubscribe. Below is two sub buttons and event script.

``````<script src="https://apis.google.com/js/platform.js"></script>

<div id="chan-1" class="g-ytsubscribe" data-channel="SMOSH" data-layout="default" data-count="default" data-onytevent="onYtEvent"></div>
</div>

<div id="chan-2" class="g-ytsubscribe" data-channel="Apple" data-layout="default" data-count="default" data-onytevent="onYtEvent"></div>
</div>

<script>

// Need to display subscribed channel name

} else if (payload.eventType == 'unsubscribe') {
// Need to display unsubscribed channel name

}
if (window.console) { // for debugging only