---
id: 6a0dcd03ee4e68698080ef67
title: "Challenge 303: Roommates"
challengeType: 28
dashedName: challenge-303
---

# --description--

Given an array of people and their roommate group, return the room assignments for a hotel stay using the following rules:

- Each person has a `name` and a `group` property:

```json
[
  { "name": "Alice", "group": "A" },
  { "name": "Bob", "group": "B" },
  { "name": "Carol", "group": "A" }
]
```

- People can only share a room with someone from the same group and are paired in the order they are given.
- Return an array of strings with names separated by `" and "` for a shared room, and just the name for a solo room. Names must appear in the order they were paired. For the example above, return `["Alice and Carol", "Bob"]`.

# --hints--

`getRoommates([{ "name": "Alice", "group": "A" }, { "name": "Bob", "group": "B" }, { "name": "Carol", "group": "A" }])` should return `["Alice and Carol", "Bob"]`.

```js
assert.deepEqual(getRoommates([{ "name": "Alice", "group": "A" }, { "name": "Bob", "group": "B" }, { "name": "Carol", "group": "A" }]).sort(), ["Alice and Carol", "Bob"].sort());
```

`getRoommates([{ "name": "John", "group": "C" }, { "name": "Julia", "group": "C" }, { "name": "Jim", "group": "C" }])` should return `["John and Julia", "Jim"]`.

```js
assert.deepEqual(getRoommates([{ "name": "John", "group": "C" }, { "name": "Julia", "group": "C" }, { "name": "Jim", "group": "C" }]).sort(), ["John and Julia", "Jim"].sort());
```

`getRoommates([{ "name": "Adam", "group": "D" }, { "name": "Abraham", "group": "E" }, { "name": "Austin", "group": "E" }, { "name": "Augustus", "group": "D" }, { "name": "Angelica", "group": "D" }, { "name": "Aaron", "group": "E" }])` should return `["Adam and Augustus", "Angelica", "Abraham and Austin", "Aaron"]`.

```js
assert.deepEqual(getRoommates([{ "name": "Adam", "group": "D" }, { "name": "Abraham", "group": "E" }, { "name": "Austin", "group": "E" }, { "name": "Augustus", "group": "D" }, { "name": "Angelica", "group": "D" }, { "name": "Aaron", "group": "E" }]).sort(), ["Adam and Augustus", "Angelica", "Abraham and Austin", "Aaron"].sort());
```

`getRoommates([{ "name": "Frank", "group": "A" }, { "name": "Emitt", "group": "B" }, { "name": "Daria", "group": "F" }, { "name": "Charles", "group": "D" }, { "name": "Bailey", "group": "A" }, { "name": "Albert", "group": "F" }])` should return `["Frank and Bailey", "Emitt", "Daria and Albert", "Charles"]`.

```js
assert.deepEqual(getRoommates([{ "name": "Frank", "group": "A" }, { "name": "Emitt", "group": "B" }, { "name": "Daria", "group": "F" }, { "name": "Charles", "group": "D" }, { "name": "Bailey", "group": "A" }, { "name": "Albert", "group": "F" }]).sort(), ["Frank and Bailey", "Emitt", "Daria and Albert", "Charles"].sort());
```

`getRoommates([{ "name": "Kevin", "group": "A" }, { "name": "Yuri", "group": "A" }, { "name": "Hugo", "group": "B" }, { "name": "Violet", "group": "A" }, { "name": "Brett", "group": "A" }, { "name": "Wayne", "group": "B" }])` should return `["Kevin and Yuri", "Violet and Brett", "Hugo and Wayne"]`.

```js
assert.deepEqual(getRoommates([{ "name": "Kevin", "group": "A" }, { "name": "Yuri", "group": "A" }, { "name": "Hugo", "group": "B" }, { "name": "Violet", "group": "A" }, { "name": "Brett", "group": "A" }, { "name": "Wayne", "group": "B" }]).sort(), ["Kevin and Yuri", "Violet and Brett", "Hugo and Wayne"].sort());
```

# --seed--

## --seed-contents--

```js
function getRoommates(people) {

  return people;
}
```

# --solutions--

```js
function getRoommates(people) {
  const groups = {};

  for (const { name, group } of people) {
    (groups[group] = groups[group] || []).push(name);
  }

  return Object.values(groups).flatMap(group => {
    const rooms = [];
    for (let i = 0; i < group.length; i += 2) {
      rooms.push(group.slice(i, i + 2).join(" and "));
    }
    return rooms;
  });
}
```
