Every group trip ends the same way. Someone opens a spreadsheet, five people stare at it, and the WhatsApp group fills with "wait, I paid for the cab on day 2??"
In Tripbae, our travel app for group trips, the Split tab answers one question: who pays whom, and how much, so everyone is square? Ideally with as few payments as possible, because nobody wants to send ₹137 to four different people.
Here's how it works, in about 25 lines of JavaScript.
Step 1: Store expenses simply
Each expense is stored with just what happened:
{ desc: 'Dinner at the lake', amount: 2400, paidBy: 'Arjun', split: ['Arjun', 'Priya', 'Kabir', 'Meera'], // who shares this cost cat: 'food', date: '2026-11-22T20:15:00Z' } split is just a list of names (a PostgreSQL text array). If it's empty, the expense is split among everyone on the trip.
Notice what we don't store: balances or settlements. Those are always calculated fresh from the list of expenses. Edit an expense, delete one or duplicate one, and the "who owes whom" view updates instantly, with no stored totals that can drift out of sync.
Step 2: Turn expenses into one number per person
For each expense, the payer is owed the full amount, and everyone in the split owes their share:
const balances = {}; members.forEach(m => { balances[m] = 0; }); expenses.forEach(e => { const splitNames = e.split?.length ? e.split : members; const share = e.amount / splitNames.length; splitNames.forEach(m => { balances[m] -= share; }); balances[e.paidBy] += e.amount; }); After this loop, each person has a single net balance:
- positive: they paid more than their share, so they get money back
- negative: they owe money
- zero: settled
For example, after a weekend:
| Person | Net balance |
|---|---|
| Arjun | +3,000 |
| Priya | +500 |
| Kabir | −1,500 |
| Meera | −2,000 |
The balances always add up to zero, because every rupee someone owes, someone else paid.
Step 3: Match the people who owe with the people who are owed
Now we pair debtors with creditors. The approach is greedy: always match the person who owes the most with the person who is owed the most.
const settlements = []; const bal = { ...balances }; // people who owe, biggest debt first const debtors = members.filter(m => bal[m] < -0.01).sort((a, b) => bal[a] - bal[b]); // people who are owed, biggest credit first const creditors = members.filter(m => bal[m] > 0.01).sort((a, b) => bal[b] - bal[a]); let d = 0, c = 0; while (d < debtors.length && c < creditors.length) { const from = debtors[d], to = creditors[c]; const amt = Math.min(-bal[from], bal[to]); settlements.push({ from, to, amt }); bal[from] += amt; bal[to] -= amt; if (Math.abs(bal[from]) < 0.01) d++; if (Math.abs(bal[to]) < 0.01) c++; } On the example above:
- Meera owes 2,000 and Arjun is owed 3,000. Meera → Arjun: 2,000. Meera is done, and Arjun is still owed 1,000.
- Kabir owes 1,500 and Arjun is owed 1,000. Kabir → Arjun: 1,000. Arjun is done, and Kabir still owes 500.
- Kabir owes 500 and Priya is owed 500. Kabir → Priya: 500. Everyone is square.
Three payments, compared with the many back-and-forth transfers you'd get by settling every single expense separately.
Why this works well
Every step fully settles at least one person: either the debtor pays off everything, or the creditor gets everything back. So with n people who aren't settled, you never need more than n − 1 payments.
An honest note
Is this always the absolute minimum number of payments? No. Finding the true minimum is a much harder problem: it means finding groups of people whose balances cancel out exactly, and the cost explodes as groups get bigger. The greedy approach is instant, easy to reason about, and in practice gives the same answer or close to it for real trip groups of 3–10 friends. For a travel app, that's the right trade-off.
The 0.01 thresholds
Splitting ₹1,000 three ways gives 333.333… Floating-point math leaves tiny leftovers like 0.0000001, and without a tolerance the loop would create a "pay ₹0.00" settlement. Treating anything under 0.01 as zero fixes that. In the UI we go further: anyone within ₹0.50 of zero shows as settled ✓, because no friend is chasing 40 paise.
Step 4: Multiple currencies
Trips cross borders, and Tripbae supports 25 currencies. The trick is to store every expense in one fixed currency per trip, chosen once when the trip's Split tab is first opened, usually the destination's currency.
- When you add an expense in the currency you're viewing, it's converted with a live exchange rate before saving.
- When showing amounts, we convert back from that stored currency to whatever you're viewing.
- The Insights tab also shows the total in your home currency, so "₫4,200,000" becomes a number you understand.
Because all the math in Steps 2–3 runs on one currency, settlements are never mixed up. Currency is only a display concern.
Bonus: not spamming your friends
When someone adds an expense, everyone else in the trip gets a push notification. But people tend to log expenses in bursts at the end of the day, and five pings in a row is annoying.
So notifications from the same person, in the same trip, within 60 seconds are batched into one ("Arjun logged 4 expenses"). The server also checks the trip budget after each expense and alerts everyone once when spending crosses 85% and once at 100%, by comparing the total before and after the new expense, so the same alert is never sent twice.
Takeaways
- Store what happened (who paid, who shares), not derived totals. Calculate balances fresh every time.
- Net balances collapse any number of expenses into one number per person.
- Greedy matching of the largest debtor with the largest creditor gives at most n − 1 payments instantly. It's not always the theoretical minimum, but it's practically right.
- Use a small tolerance for floating-point leftovers.
- Do all the math in one currency, and convert only for display.
Tripbae is a free AI travel app for group trips: plan with Lumi, split expenses in any currency with no daily limit, and share one trip album. tripbae.in · Android


