Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To remove duplicates from array in react native, use the new Set() method with it will create new array without dublicate elements.

The Set object lets you store unique values of any type, whether primitive values or object references.

To use new set in array you have to follow below syntax.

[...new Set(arr)]

Today, I’m going to show you How do I remove duplicates from array in react native, as above mentioned, I’m going to use the above-mentioned new Set() method to get only unique elements of array.

Let’s start today’s tutorial how do you remove duplicates from array in react native?

React native remove duplicates from array example

Here, we will create an array with some data, and then we will use the new Set() method to remove duplicates elements and store in a different variable.

import React, { useState, useEffect } from "react";
import { View, Text } from "react-native";
export default function App() {
  const [arr, setArr] = useState([
    "Infinitbility",
    "aguidehub",
    "sortoutcode",
    "sortoutcode",
    "Infinitbility"
  ]);

  useEffect(() => {
    setArr([...new Set(arr)]);
  }, []);

  return (
    <View>
      <Text>{JSON.stringify(arr)}</Text>
    </View>
  );
}

In the above example, I have created an array state with dublicate elements and make unique in a useEffect after that printed result in a screen.

I hope it helps you, All the best đź‘Ť.

React Native, remove duplicates from array example
React Native, remove duplicates from array example

Try it yourself